这对你来说可能是一个简单的破解,但我不明白为什么这段代码不会导致在我的 Android 外部文件系统中创建一个新文件?目录是正确创建的,但不是文件,而是我得到一个 java.io.FileNotFoundException:/mnt/sdcard/Mydir/MyFile,任何人都可以看到问题是什么,因为我无法看到它?
//Generate a unique filename using date and time
String fileName = "myFile_"+formatter_file_name.format(today)+".txt";
//Get path to root
String root = Environment.getExternalStorageDirectory().toString();
//Create(if not exists) directory in root in which to store the reports
File myDir = new File(root + "/myDir");
myDir.mkdirs();
//Create report file object
File file = new File (myDir, fileName);
//If file already exists delete id(this should not be able to happen)
if (file.exists()) file.delete();
try {
FileOutputStream fout = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fout);
// Write the string to the file
osw.write("TEST STRING");
//Ensure that everything has been written to the file and close
osw.flush();
osw.close();
} catch (Exception e) {
e.printStackTrace();
}
我的清单中有正确的权限,我将插入一个检查以查看外部存储是否可用于写入,但我认为这不是问题......