Manav Dutta
Manav Dutta

Reputation: 171

How to read a file in Android

I have a text file called "high.txt". I need the data inside for my Android app. But I have absolutely no idea how to read it into an ArrayList of the Strings. I tried the normal way of doing it in Java but apparently that doesn't work in Android since it cant find the file. So how do I go about doing this? I have put it in my res folder. But how do you take the input stream that you get from opening the file within Android and read it into an ArrayList of Strings. I am stuck on that part.

Basically it would look something like this:

3.  What do you do for an upcoming test?
L: make sure I know what I'm studying and really review and study for this thing. Its what Im good at. Understand the material really well.
CL: Time to study. I got this, but I really need to make sure I know it,
M: Tests can be tough, but there are tips and tricks. Focus on the important, interesting stuff. Cram in all the little details just to get past this test. 
CR: -sigh- I don't like these tests. Hope I've studied enough to pass or maybe do well.
R: Screw the test. I'll study later, day before should be good.

This is for a sample question and all the lines will be stored as separate strings in the array list.

Upvotes: 0

Views: 261

Answers (3)

deadfish
deadfish

Reputation: 12304

//find all files from folder /assets/txt/

String[] elements;

try {
    elements = getAssets().list("txt");
} catch (IOException e) {
    e.printStackTrace();
}

//for every files read text per line
for (String fileName : elements) {
    Log.d("xxx", "File: " + fileName);

    try {
        InputStream open = getAssets().open("txt/" + fileName);
        InputStreamReader inputStreamReader = new InputStreamReader(open);
        BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
        String line = "";

        while ((line = bufferedReader.readLine()) != null) {
            Log.d("xxx", line);     
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

Upvotes: 0

Solution
Solution

Reputation: 602

If you want to read file from External storage than use below method.

public void readFileFromExternal(){
    String path = Environment.getExternalStorageDirectory().getPath()
               + "/AppTextFile.txt";
    try {
        BufferedReader reader = new BufferedReader(new FileReader(path));
        String line, results = "";
        while( ( line = reader.readLine() ) != null)
        {
            results += line;
        }
        reader.close();
                Log.d("FILE","Data in your file : " + results);
      } catch (Exception e) {
    }
}

Upvotes: 0

Strigger
Strigger

Reputation: 1903

If you put the text file in your assets folder you can use code like this which I've taken and modified from one of my projects:

public static void importData(Context context) {
    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(context.getAssets().open("high.txt")));

        String line;

        while ((line = br.readLine()) != null) {
            String[] columns = line.split(",");

            Model model = new Model();

            model.date = DateUtil.getCalendar(columns[0], "MM/dd/yyyy");
            model.name = columns[1];

            dbHelper.insertModel(model);
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
}

Within the loop you can do anything you need with the columns, what this example is doing is creating an object from each row and saving it in the database.

For this example the text file would look something like this:

15/04/2013,Bob
03/03/2013,John
21/04/2013,Steve

Upvotes: 1

Related Questions