简体   繁体   中英

Reading text-file in res/raw, very slow. Using BufferedReader

The size of the text-file is 560 kilobytes and has about 24500 rows. Each row is added to a list. Maybe my phone is to old and slow? Phone-model: Samsung GT-S5570 with Android 2.3.4. It takes about 30 seconds or more to read it and I'm pretty sure that my algorithms outside the Reader-class is not the problem. Anyone else who has encountered a similar problem or has an idea of what the problem could be?

        public class Reader {  

        public List<String> read(String file) {  
        Context ctx = ApplicationContextProvider.getContext();  
        List<String> entries = new ArrayList<String>();  

        //Get res/raw text-file id.  
        int resId = ctx.getResources().getIdentifier(file,"raw", ctx.getPackageName());  
        InputStream inputStream = ctx.getResources().openRawResource(resId);  

        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream), 8192);  

        try {  
             String test;  
             while (true) {  
                 test = reader.readLine();  

                 if (test == null)  
                     break;  
                 entries.add(test);  
            }  
            inputStream.close();  
            reader.close();  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  

        return entries;  
    }  
} 

The device you describe is a very old and slow device. You should take care to speedup the reading process as much as possible by:

  • Initializing the ArrayList to be about the size of expected entries so it doesn't resize itself several times during addition of entries. This involves a lot of operations several times while the array grows as it has to copy all its previous elements again when growing.
  • Read the whole file at once and then parse the result.

It is also a good idea to measure how much time each part takes - reading the file and inserting the 24500 strings into the ArrayList. Bad performance may come from the least expected direction.

Please try the following approach and share the results (if possible with time measurements):

private char[] readWholeFile(String file) {
    Context ctx = ApplicationContextProvider.getContext();
    int resId = ctx.getResources().getIdentifier(file, "raw", ctx.getPackageName());
    InputStream inputStream = ctx.getResources().openRawResource(resId);

    BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream), 8192);
    try {
        int length = inputStream.available();
        char[] contents = new char[length];
        reader.read(contents, 0, length);
        return contents;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    } finally {
        try {
            if (reader != null) reader.close();
        } catch (Exception e) {

        }
    }
}


public List<String> readEntries() {
    final int EXPECTED_ELEMENTS = 24500;

    char[] contents = readWholeFile("somefile");
    if (contents == null) {
        return new ArrayList<String>();
    }
    List<String> entries = new ArrayList<String>(EXPECTED_ELEMENTS);
    String test;
    BufferedReader reader = new BufferedReader(new CharArrayReader(contents));
    try {
        while ((test = reader.readLine()) != null) {
            entries.add(test);
        }
        return entries;
    } catch (IOException e) {
        e.printStackTrace();
        return new ArrayList<String>();
    } finally {
        try {
            if (reader != null) reader.close();
        } catch (Exception e) {

        }
    }
}

If you use IOUtils from apache "commons-io" it's even easier.

InputStream is = getResources().openRawResource(R.raw.yourNewTextFile);
String s = IOUtils.toString(is);
IOUtils.closeQuietly(is); // don't forget to close your streams

you can download them from http://commons.apache.org/proper/commons-io/ or http://mvnrepository.com/artifact/commons-io/commons-io

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM