다음과 같이 최선의 해결책이 될 것 같습니다. 텍스트가 정적이라고 가정하고 이미 txt 파일에 포함되어 있습니다.
res/raw/textfile.txt
과 비슷한 경로로 텍스트 파일을 응용 프로그램에 저장하십시오.
이러한 파일에 액세스하려면 다음과 유사한 코드를 사용하십시오. R.raw.textfile
은 열리는 파일을 결정하므로 사용자가 주 활동에서 선택한 항목에 따라 변경해야합니다.
// Call the LoadText method and pass it the resourceId
LoadText(R.raw.textfile);
public void LoadText(int resourceId) {
// The InputStream opens the resourceId and sends it to the buffer
InputStream is = this.getResources().openRawResource(resourceId);
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String readLine = null;
StringBuilder result = new StringBuilder();
try {
while ((readLine = br.readLine()) != null) {
result.append(readLine);
}
// Close the InputStream and BufferedReader
is.close();
br.close();
//Do something with result.toString(); (set a text view's content to result.toString())
} catch (IOException e) {
e.printStackTrace();
}
}
답변을 주셔서 감사합니다/오케이 괜찮은 자바에 대한 .... 그 XML 파일은 어떻게해야합니까 ?? – Androidman
이렇게하려면 strings.xml과 같은 xml 파일을 사용할 필요가 없습니다. 'txt' 파일을'res' 폴더에 넣으면 자동적으로 접근 가능해야합니다! 추신 : 만약이 도움이 받아 들여진 대답으로 나를 선택하십시오 :) –
내 질문은 어떻게해야 myfile.xml에 그 mtq.txt를 호출해야합니까? myfile.java에서 말한 것처럼 코드를 작성했습니다. – Androidman