gpt4 book ai didi

java - 如何使用 FileWriter 在文件中附加文本

转载 作者:行者123 更新时间:2023-12-02 04:21:47 26 4
gpt4 key购买 nike

我正在尝试继续将文本添加到我的文件(内部存储)中使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com