您好我尝试在java中创建一个文件,在java中写入并在ti中检索以读取它内部。但它失败了,任何人都可以帮助我吗? 这是我的代码。
public void saveAndRetrieve () {
try {
File sdcard = Environment.getExternalStorageDirectory();
FileWriter writer = new FileWriter(sdcard + getFilesDir().getPath().toString() + "/scores.txt");
writer.append("score: test");
writer.flush();
writer.close();
//Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
StringBuilder text = new StringBuilder();
try {
File sdcard = Environment.getExternalStorageDirectory();
File file = new File(sdcard,sdcard + getFilesDir().getPath().toString() + "/scores.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
text.append(line);
Log.d("Test", "text : "+text+" : end");
text.append('\n');
}
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
我试图修复的例外是:。
W / System:ClassLoader引用了未知路径: /data/app/com.example.mathieu.whereswaldo-2/lib/x86 W / System.err: java.io.FileNotFoundException: /storage/emulated/0/data/user/0/com.example.mathieu.whereswaldo/files/scores.txt (没有这样的文件或目录)W / System.err:at java.io.FileOutputStream.open(Native Method)W / System.err:at java.io.FileOutputStream中。(FileOutputStream.java:221) W / System.err:at java.io.FileOutputStream中。(FileOutputStream.java:108) W / System.err:在java.io.FileWriter。(FileWriter.java:63) W / System.err:at com.example.mathieu.whereswaldo.MainActivity.save(MainActivity.java:64) W / System.err:at com.example.mathieu.whereswaldo.MainActivity.onCreate(MainActivity.java:96)