I am trying to write to a file in an Android app with this code:
File save = new File("sdcard/save.txt");
if(!save.exists()) {
try {
save.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
try {
FileOutputStream fos = new FileOutputStream(save);
OutputStreamWriter osw = new OutputStreamWriter(fos);
osw.write("1");
osw.flush();
osw.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
However, my debugger goes to the IOException after the line osw.close();
The problem is by that stage e doesn’t exist, so I cant read the exception message.
I added the right premission in the androidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
but it doesn’t work.
use below code