user1635224 user1635224 - 1 year ago 141
Java Question

How can I read a text file in Android?

I want to read the text from a text file. In the code below, an exception occurs (that means it goes to the

block). I put the text file in the application folder. Where should I put this text file (mani.txt) in order to read it correctly?

InputStream instream = openFileInput("E:\\test\\src\\com\\test\\mani.txt");
if (instream != null)
InputStreamReader inputreader = new InputStreamReader(instream);
BufferedReader buffreader = new BufferedReader(inputreader);
String line,line1 = "";
while ((line = buffreader.readLine()) != null)
}catch (Exception e)
catch (Exception e)
String error="";

Answer Source

try this :

i assume your text file is on sd card

    //Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"file.txt");

//Read text from file
StringBuilder text = new StringBuilder();

try {
    BufferedReader br = new BufferedReader(new FileReader(file));
    String line;

    while ((line = br.readLine()) != null) {
catch (IOException e) {
    //You'll need to add proper error handling here

//Find the view by its id
TextView tv = (TextView)findViewById(;

//Set the text

following links can also help you :

How can I read a text file from the SD card in Android?

How to read text file in Android?

Android read text raw resource file

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download