이 질문은 중복 된 것일 수 있습니다. 나는 그것을 유감스럽게 생각한다. 비슷한 게시물을 여러 번 보았지만 일을 끝내지 못했습니다.내부 메모리에 디렉토리를 만들고 파일을 저장하십시오.
내 응용 프로그램에서 Json 데이터를 .txt 파일에 저장하고 내부 메모리의 특정 폴더에 저장하려고합니다. 또는 파일을 Android/data 폴더에 직접 저장하는 것도 괜찮습니다.
다음 코드를 사용하여 저장했지만 원하는 파일을 찾을 수 없습니다. 아무도 내 코드와 teh 문제가 무엇인지 말해 줄 수 있고 어떻게 그것을 수정할 수 있습니까 ??
String fname, fcontent;
Context context;
ProgressDialog pDialog;
public SaveIntoFile(String fileName, String jsonStr, Context context, ProgressDialog pDialog) {
this.fname = fileName;
this.context = context;
this.fcontent = jsonStr;
this.pDialog = pDialog;
}
public void write() {
try {
File mydir = context.getDir("NepalJapan", Context.MODE_PRIVATE);
File file = new File(mydir, fname+".txt");
if (file.exists())
file.delete();
// If file does not exists, then create it
if (!file.exists()) {
file.createNewFile();
}
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(fcontent);
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public String read() throws IOException {
BufferedReader br = null;
String response = null;
StringBuffer output = new StringBuffer();
File mydir = context.getDir("NepalJapan", Context.MODE_PRIVATE);
File file = new File(mydir, fname+".txt");
if (!file.exists()) {
if (pDialog.isShowing())
pDialog.dismiss();
return null;
} else {
br = new BufferedReader(new FileReader(file));
String line = "";
while ((line = br.readLine()) != null) {
output.append(line + "\n");
}
response = output.toString();
br.close();
return response;
}
}
오류가 있습니까? logcat에 –
매니페스트 파일에 읽기/쓰기 권한이 있습니까? – sUndeep
@RandykaYudhistira logcat에 오류가 표시되지 않습니다. 앱은 잘 실행되지만 파일을 저장하지는 않습니다. –