0

Currently I have this code where I copy paste from here into my source code. But due to some reason unknown to me, the OutputStreamWriter keep giving IOException. And I'm clueless on what cause this and how to fix it. Please help. Below is my code.

 public void generateNoteOnSD(){// create a File object for the parent directory
    try {
        File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
        File myFile = new File(path, "mytextfile.txt");
        FileOutputStream fOut = new FileOutputStream(myFile,true);
        OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);
        myOutWriter.append("the text I want added to the file");

        Log.d("Save File  Test : ","Success");

        myOutWriter.close();
        fOut.close();
    }
    catch (IOException e) {
        //do something if an IOException occurs.
        Log.d("Save File Test : ","Failed");
    }
}

I have already included

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

in the manifest, and also running the code in AsyncTask - onPostExecute and on AVD with Lollipop version.

My stacktrace:

08-28 11:22:52.788 30478-30478/com.example.azrie.dummyvoice E/YOUR_APP_LOG_TAG: I got an error
java.io.FileNotFoundException: /storage/emulated/0/Download/mytextfile.txt: open failed: EACCES (Permission denied)
at libcore.io.IoBridge.open(IoBridge.java:452)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
at com.example.azrie.dummyvoice.ReadHTML.generateNoteOnSD(ReadHTML.java:126)
at com.example.azrie.dummyvoice.ReadHTML.onPostExecute(ReadHTML.java:78)
at com.example.azrie.dummyvoice.ReadHTML.onPostExecute(ReadHTML.java:37)
at android.os.AsyncTask.finish(AsyncTask.java:651)
at android.os.AsyncTask.-wrap1(AsyncTask.java)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
at libcore.io.Posix.open(Native Method)
at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
at libcore.io.IoBridge.open(IoBridge.java:438)
at java.io.FileOutputStream.<init>(FileOutputStream.java:87) 
at com.example.azrie.dummyvoice.ReadHTML.generateNoteOnSD(ReadHTML.java:126) 
at com.example.azrie.dummyvoice.ReadHTML.onPostExecute(ReadHTML.java:78) 
at com.example.azrie.dummyvoice.ReadHTML.onPostExecute(ReadHTML.java:37) 
at android.os.AsyncTask.finish(AsyncTask.java:651) 
at android.os.AsyncTask.-wrap1(AsyncTask.java) 
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Community
  • 1
  • 1
azriebakri
  • 1,131
  • 2
  • 11
  • 36

1 Answers1

-1
private static final String FILE_NAME="score.txt";
BufferedOutputStream o;
                try {
                    o = new BufferedOutputStream(new FileOutputStream(new File(getFilesDir(),FILE_NAME)));
                    String s = //here write what you want to write to the file . its got to be string to work
                    o.write(s.getBytes());
                    o.close();
                }catch (FileNotFoundException e ){

                }catch (IOException e){

                }

for me its work for every time and its not opening new file every time. if you want to see it in action serch FightWithMath in the play store. you will need to fully restart the app after first login and its writing in the main screen and while the game running every time you win

Finci
  • 108
  • 1
  • 10