我正在尝试继续将文本添加到我的文件(内部存储)中 使用 FileOutputStream 尝试过,效果很好,但在我更改为使用 FileWriter 后,我开始出现只读文件系统错误
EditText edd = findViewById(R.id.editTextData);
Spinner spp = findViewById(R.id.spinnerCategory);
String text1 = edd.getText().toString();
String text2 = spp.getSelectedItem().toString();
String filepath = text2 + ".txt";
try {
BufferedWriter bw = new BufferedWriter(new FileWriter(filepath, true));
bw.write(text1);
bw.newLine();
bw.close();
Toast.makeText(getBaseContext(), "Information Saved", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
} //End try catch statement
}```
It's supposed to say Information Saved but i keep getting read-only file system
最佳答案
EditText edd = findViewById(R.id.editTextData);
Spinner spp = findViewById(R.id.spinnerCategory);
String text1 = edd.getText().toString();
String text2 = spp.getSelectedItem().toString();
String filename = text2 + ".dat";
try {
File fa = new File(getFilesDir(),filename); //getting the filename path
FileWriter fw = new FileWriter(fa,true);
fw.write(text1 + "\n");
fw.close();
Toast.makeText(getBaseContext(), "Information Saved", Toast.LENGTH_SHORT).show();
} catch (Exception e)
{
Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
} //End try catch statement
关于java - 如何使用 FileWriter 在文件中附加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56632778/