Reading a text file line by line in android

user3668496 picture user3668496 · Jun 18, 2014 · Viewed 37.6k times · Source

hi i just started learning android development and i'm trying to build an app that reads text from files. i have been searching all over the internet but i don't seem to find the way to do so , so i have a few questions..

1.how to do this? what is the preferred way to read a file line by line in android?

2.where should i store the file? should it be in the raw folder or maybe in the assets folder?

so this is what i already tried: " (i think the problem may be with finding the file..)

@Override   
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.filereader);

    try {
        // open the file for reading
        InputStream fis = new FileInputStream("text.txt");

        // if file the available for reading
        if (fis != null) {

          // prepare the file for reading
          InputStreamReader chapterReader = new InputStreamReader(fis);
          BufferedReader buffreader = new BufferedReader(chapterReader);

          String line;

          // read every line of the file into the line-variable, on line at the time
          do {
             line = buffreader.readLine();
            // do something with the line 
             System.out.println(line);
          } while (line != null);

        }
        } catch (Exception e) {
            // print stack trace.
        } finally {
        // close the file.
        try {
            fis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Answer

TyMarc picture TyMarc · Jun 18, 2014

Depends on what you intend to do with that file. If your goal is only to read the file, then the asset folder is the way to go. If you want to store information in that file when you are done working with it, you should put it on the device.

If you choose option number 2, you need to decide if you want other applications to read the file. More information can be found at this address:

http://developer.android.com/training/basics/data-storage/files.html

Else, you can read/write directly to the device with the standard java procedure just like you described. Though, the filepath would probably be

"/sdcard/text.txt"

EDIT:

Here's some piece of code to get started with

FileInputStream is;
BufferedReader reader;
final File file = new File("/sdcard/text.txt");

if (file.exists()) {
    is = new FileInputStream(file);
    reader = new BufferedReader(new InputStreamReader(is));
    String line = reader.readLine();
    while(line != null){
        Log.d("StackOverflow", line);
        line = reader.readLine();
    }
}

But it assumes that you know you've put the text.txt at the root of your sdcard.

If the file is in the assets folder, you have to do this:

BufferedReader reader;

try{
    final InputStream file = getAssets().open("text.txt");
    reader = new BufferedReader(new InputStreamReader(file));
    String line = reader.readLine();
    while(line != null){
        Log.d("StackOverflow", line);
        line = reader.readLine();
    }
} catch(IOException ioe){
    ioe.printStackTrace();
}