0

I'm trying to keep adding text into my file (internal storage) it was tried using FileOutputStream and it was fine but after i changed to use FileWriter then i started having the read-only file-system error

        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
Zoe
  • 27,060
  • 21
  • 118
  • 148
  • Which line is causing the exception? Alternative, can you paste the stack trace? – Dhruvil Vaghela Jun 17 '19 at 13:59
  • It seems there is no stack trace but i'm getting this from debug **java.io.FileNotFoundException: Food (Read-only file system).** This creates a file right? `BufferedWriter bw = new BufferedWriter(new FileWriter(filepath, true));` as i want to create 3 different files with different text inside them – Ultimate_Asylum Jun 17 '19 at 15:24
  • This clearly states that you don't have permission to write to storage as it's a read only file system. Probably, this can help https://stackoverflow.com/questions/9372805/java-io-ioexception-read-only-file-system – Dhruvil Vaghela Jun 18 '19 at 12:10
  • i tried it and it works now, thanks alot – Ultimate_Asylum Jun 19 '19 at 11:40
  • Request you to please post the solution as answer so it can help other users. – Dhruvil Vaghela Jun 19 '19 at 12:40

1 Answers1

0
    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