Below is the code to read a text file from SD card.
First, we have to define path of the text file to a File.
Second step is to use BufferedReader class to read line by line.
File sdcard = Environment.getExternalStorageDirectory();
File readFile = new File(sdcard,"reading.txt");
//Read text from file
StringBuilder stringBuilder = new StringBuilder();
try {
BufferedReader bufferedReader = new BufferedReader(new FileReader(readFile));
String str;
while ((str = bufferedReader.readLine()) != null) {
stringBuilder.append(line);
stringBuilder.append('\n');
}
bufferedReader.close();
}
catch (IOException e) {
System.out.println("Error : "+e.getMessage());
}
TextView messageFile = (TextView)findViewById(R.id.message_text);
messageFile.setText(text.toString());
0 Comment(s)