我创建了一个应用程序,我只是添加了将数据导出为 .txt 或 .csv 文件的功能,但在创建文件时出现错误。logcat 中的错误显示为File write failed: java.io.IOExcepion: open failed: ENOENT (NO such file or directory)
.
谁能看到我在哪里犯了错误?
我调用 writeToFile() 方法传递 String txt;或字符串 csv;根据需要的文件类型放入其中。
代码:
String txt = ".txt";
String csv = ".csv";
private void writeToFile(String fileType) {
// if file name has not been entered...
if (filename.equals("")||filename.equals(null)) {
//display toast
toastMsg = "Please enter a file name";
toast();
}
// else if file name has been entered...
else if (!filename.equals("")) {
// create FileCheck
File fileCheck = new File("/sdcard/" + filename + fileType);
// if file exists...
if (fileCheck.exists()) {
// display toast
toastMsg = "Export Error: File already exists";
toast();
}
// if file does not exist...
else if (!fileCheck.exists()) {
try {
// create file
File myFile = new File("/sdcard/" + filename + fileType);
myFile.createNewFile();
FileOutputStream fOut = new FileOutputStream(myFile);
OutputStreamWriter myOSW = new OutputStreamWriter(fOut);
myOSW.append("file data here");
myOSW.close();
fOut.close();
// display toast
toastMsg = "file created: " + myFile.toString();
toast();
}
catch (IOException e) {
Log.e(TAG, "File write failed: " + e.toString());
toastMsg = "file write failed: " +e.toString();
toast();
}
}
}
}