Read Text-File from SD-Card in Android
by Sasikumar[ Edit ] 2013-10-23 21:47:32
To Read a Text-File from SD-Card in Android use the following code:
try {
File sdcardfile = new File(Environment.getExternalStorageDirectory() + "/example.txt");
fileIS = new FileInputStream(sdcardfile);
BufferedReader buffer1 = new BufferedReader(new InputStreamReader(fileIS));
String stringRead = new String();
while ((stringRead = buffer1.readLine()) != null) {
Log.d("line: ", stringRead);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
This is enclosed in a try-catch block as a safety, because if the file is not found we could catch the exception in the catch block.
In this "example.txt" is the text file from which the contents had to be read.
The String "stringRead" contains the contents read from the text file.