0% found this document useful (0 votes)
13 views

How To Read Write String From A File in Android

This document describes how to write and read strings from a file in Android. It shows code to write a string to a file using FileOutputStream, and then read the string back from the file using InputStream and BufferedReader. The write method opens a file for writing, writes the string bytes, and closes the stream. The read method opens the file as an input stream, reads it line by line into a StringBuilder, and returns the full string.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

How To Read Write String From A File in Android

This document describes how to write and read strings from a file in Android. It shows code to write a string to a file using FileOutputStream, and then read the string back from the file using InputStream and BufferedReader. The write method opens a file for writing, writes the string bytes, and closes the stream. The read method opens the file as an input stream, reads it line by line into a StringBuilder, and returns the full string.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as ODT, PDF, TXT or read online on Scribd
You are on page 1/ 1

How to read write string from a file in android

private void WriteSomeTextToAFile () {


String filename = "myfile";
String string = "Bonjour le monde!";
FileOutputStream outputStream;
try {
outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
outputStream.write(string.getBytes());
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
private String readFromFile () {
String ret ="";
try {
InputStream inputStream = openFileInput("myfile");
if (inputStream != null){
InputStreamReader inputStreamReader = new
InputStreamReader(inputStream);
StringBuilder stringBuilder = new StringBuilder();
String receiveString;
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
while ( (receiveString = bufferedReader.readLine()) != null ) {
stringBuilder.append(receiveString);
}
inputStream.close();
ret = stringBuilder.toString();
}
} catch (IOException e) {
e.printStackTrace();
}
return ret;
}

You might also like