-1

I'm learning to write and read files from the internal storage on Android. I have this code:

String fileName = "MyFile";
String content = "hello world";

FileOutputStream outputStream = null;
try {
outputStream = openFileOutput(fileName, Context.MODE_PRIVATE);
outputStream.write(content.getBytes());
    outputStream.close();
} catch (Exception e) {
  e.printStackTrace();
}

Where is my file stored and how to read it?

SuperMario32
  • 70
  • 1
  • 1
  • 9

2 Answers2

0

Since you made it private, with the use of openFileOutput() it's private to your app alone and should be within it's directory "/data/data/" Read: https://developer.android.com/guide/topics/data/data-storage.html https://android.stackexchange.com/questions/47924/where-android-apps-store-data

Community
  • 1
  • 1
ricol070
  • 492
  • 2
  • 11
0

The File should be saved under /data/data/Android/urpackagename/ folder.

To read

 FileInputStream in = openFileInput("filename.txt");
InputStreamReader inputStreamReader = new InputStreamReader(in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuilder sb = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
    sb.append(line);
}

For More Detail with same piece of code Write and Read openFile...() api

Community
  • 1
  • 1
Magesh Pandian
  • 8,789
  • 12
  • 45
  • 60