0

I have a button and I would like to create a file within the onClick method, but i always get a FileNotFoundException. The permission to write into internal storage is already in the manifest.xml

Here is the code:

registerButton.setOnClickListener(new OnClickListener() 
    {
        @Override
        public void onClick(View v) 
        {
            try 
            {
                bufferedWriter = new BufferedWriter(new FileWriter("sample.txt"));
                bufferedWriter.write(getString(R.string.emailString));
                bufferedWriter.newLine();
                bufferedWriter.write(getString(R.string.passwordString));
                System.out.println("Done!");
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
                Log.d("MainActivity", "Bad luck!");
            }
            finally
            {
                try
                {
                    if(bufferedWriter != null)
                        bufferedWriter.close();
                }
                catch (IOException e) 
                {
                    e.printStackTrace();
                    Log.d("MainActivity", "Sorry for that!");
                }
            }
        }
    });

Following is error log

03-28 01:00:11.537: W/System.err(2610): java.io.FileNotFoundException: /sample.txt: open failed: EROFS (Read-only file system)
03-28 01:00:11.557: W/System.err(2610): at libcore.io.IoBridge.open(IoBridge.java:406)
03-28 01:00:11.557: W/System.err(2610): at java.io.FileOutputStream.<init>(FileOutputStream.java:88)
03-28 01:00:11.567: W/System.err(2610): at java.io.FileOutputStream.<init>(FileOutputStream.java:73)
03-28 01:00:11.588: W/System.err(2610): at java.io.FileWriter.<init>(FileWriter.java:80)
03-28 01:00:11.588: W/System.err(2610): at
org.eirich.way.MainActivity$1.onClick(MainActivity.java:34)
03-28 01:00:11.607: W/System.err(2610): at android.view.View.performClick(View.java:3480)
03-28 01:00:11.607: W/System.err(2610): at android.view.View$PerformClick.run(View.java:13983)
03-28 01:00:11.628: W/System.err(2610): at android.os.Handler.handleCallback(Handler.java:605)
03-28 01:00:11.628: W/System.err(2610): at android.os.Handler.dispatchMessage(Handler.java:92)
03-28 01:00:11.647: W/System.err(2610): at android.os.Looper.loop(Looper.java:137)
03-28 01:00:11.667: W/System.err(2610): at android.app.ActivityThread.main(ActivityThread.java:4340)
03-28 01:00:11.667: W/System.err(2610): at java.lang.reflect.Method.invokeNative(Native Method)
03-28 01:00:11.687: W/System.err(2610): at java.lang.reflect.Method.invoke(Method.java:511)
03-28 01:00:11.687: W/System.err(2610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
Lucifer
  • 29,392
  • 25
  • 90
  • 143
Arthur Eirich
  • 3,368
  • 9
  • 31
  • 63

3 Answers3

3

Create file in internal storage as :

bufferedWriter = new BufferedWriter(new FileWriter(new 
                            File(getFilesDir()+"/sample.txt")));

because currently you are not passing Directory path with file.use getFilesDir() which return the absolute path to the directory on the filesystem

ρяσѕρєя K
  • 132,198
  • 53
  • 198
  • 213
  • 1
    +1 This is correct. If you want to write to `IS`, you have to use `getFilesDir()`. – Simon Dorociak Mar 30 '13 at 13:26
  • Thank you, ρяσѕρєя K, that worked for me. I created a java project and the code I provided worked actually so I thought it will work for android, too. I didn't know that I have to getFilesDir() ^^ – Arthur Eirich Mar 30 '13 at 13:29
1

It because you are not passing Directory path to FileWriter.

Use:

bufferedWriter = new BufferedWriter(new FileWriter(newFile(getFilesDir()+File.separator+"sample.txt")));

Instead:

bufferedWriter = new BufferedWriter(new FileWriter("sample.txt"));

Even check below link, it help you.

Writing/Reading Files to/from Android phone's internal memory

Community
  • 1
  • 1
RobinHood
  • 10,897
  • 4
  • 48
  • 97
0

You can write into the Internal Storage by using the openFileOutput function :

String string = "My data";

FileOutputStream os = openFileOutput("sample.txt", Context.MODE_PRIVATE);
os.write(string.getBytes());
os.close();

And make sure you have the following permission :

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Halim Qarroum
  • 13,985
  • 4
  • 46
  • 71