你好我有一个创建文件并将数据写入它的方法..现在我想将这个方法调用到另一个类来将数据附加到该文件。但是当我运行应用程序时它给了我这个错误data.xml :open failed:EROFS(只读文件系统)
这是我的方法以及我如何从第一堂课中调用它
String s1 = "hello";
String s2 = "world";
String sname = "data.xml";
saveDataToFile(s1,s2,sname)
public void saveDataToFile(String data1, String data2 ,String fileName) {
Log.d("Checks", "Trying to save data");
try {
// Set up the file directory
String filePath = Environment.getExternalStorageDirectory().toString() + "/Data Folder";
File fileDirectory = new File(filePath);
fileDirectory.mkdirs();
Log.d("Checks", "Directory created");
// Set up the file itself
File textFile = new File(fileDirectory, fileName);
textFile.createNewFile();
Log.d("Checks", "File created");
// Write to the file
FileOutputStream fileOutputStream = new FileOutputStream(textFile);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
outputStreamWriter.append(data1);
outputStreamWriter.append(data2);
outputStreamWriter.close();
fileOutputStream.close();
Toast.makeText(getApplicationContext(), "Done writing to SD card", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
} }
当我再次调用它时,我跳过设置目录和文件本身就像这样,我从第二个类这样调用它
String s1 = "hello again";
String s2 = "world again";
String sname = "data.xml";
saveDataToFile(s1,s2,sname)
public void saveDataToFile(String data1, String data2 ,String fileName) {
Log.d("Checks", "Trying to save data");
try {
// Write to the file
FileOutputStream fileOutputStream = new FileOutputStream(textFile);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(fileOutputStream);
outputStreamWriter.append(data1);
outputStreamWriter.append(data2);
outputStreamWriter.close();
fileOutputStream.close();
Toast.makeText(getApplicationContext(), "Done writing to SD card", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
}
}