SuperMario32 SuperMario32 - 1 month ago 25
Android Question

Reading and writing to files in internal storage on Android

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?

Answer

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