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

标签 java android filewriter

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

相关文章:

java - 无法正确填充 HTML 表格的列?

Java 选项卡 ("\t")无法使用 FileWriter

java - 如何使用 Sahi 和 java 最大化浏览器

java - 异步 Web 服务回调机制可以用于 Java 和 .NET 客户端吗?

java - 卷将属性文件映射到 docker 中的 webapp

java - servlet 中的可配置值

android - VLC : Cant Play Video from getExternalStorageDirectory()

android - JsonRequester 库从内部函数访问监听器

android - 如何更改复制文本时出现的文本选择工具栏颜色?

java - 将多行 JTextArea 写入 txt 文件