0

I have a problem and can't find a good solution to it. I need to read a textfile with large amount of data (file has 16MB). The file contains 12 columns with integer values in each one. Generally my problem is how to do this without freezing the app. I have my file in the assets folder of my project and I tried using something like this:

AssetManager assetManager = this.getAssets();
try {
    InputStream inputStream = assetManager.open("3333.ecg");
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
    String str;
    while ((str = bufferedReader.readLine()) != null) {
        stringBuilder.append(str);
    }
} catch (IOException e) {
    e.printStackTrace();
}

But app freezes. My goal is to get the data from each column and save it into an arraylist of integers. I'm looking for some advice.

Thanks in advance.

Morteza Jalambadani
  • 2,190
  • 6
  • 21
  • 35
Piotr
  • 71
  • 12
  • Have you tried running the code on a [background thread?](https://stackoverflow.com/questions/15472383/how-can-i-run-code-on-a-background-thread-on-android) – Tenten Ponce Jun 03 '19 at 10:16
  • 2
    the only thing you need is to do it out of main thread – Vladyslav Matviienko Jun 03 '19 at 10:18
  • You could use an `AsyncTask` to read it asynchronously. You should never to expensive operations in the main thread. – Chris623 Jun 03 '19 at 10:25
  • Thanks for the answers, Eventually I planned to do it in a separate thread but I thought I do something else wrong cause 16 mb didn't seem that large to me and the app should read the file quicker. – Piotr Jun 03 '19 at 10:29

1 Answers1

1

Use this to run it in background:

AsyncTask.execute(new Runnable() {
   @Override
   public void run() {
    AssetManager assetManager = this.getAssets();
    try {
        InputStream inputStream = assetManager.open("3333.ecg");
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
        String str;
        while ((str = bufferedReader.readLine()) != null) {
            stringBuilder.append(str);
    }
    } catch (IOException e) {
        e.printStackTrace();
    }
   }
});
Klatschen
  • 1,652
  • 19
  • 32