简体   繁体   中英

Reading a large 10mb text file in android app

I am working on a dictionary app which reads words from a text file, but the size of the text file is 10mb so I can't run it on the emulator or on a device due to memory limitations.

So what is the solution for this problem? Can I read the text file from a zip while it is compressed or is it better to split it in 10 separate text files 1mb each?

Below is the current code for reading the text file, what changes do I have to make to the code?

private synchronized void loadWords(Resources resources) throws IOException {
        if (mLoaded) return;

        Log.d("dict", "loading words");
        InputStream inputStream = resources.openRawResource(R.raw.definitions);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        try {
            String line;
            while((line = reader.readLine()) != null) {
                String[] strings = TextUtils.split(line, ":");
                if (strings.length < 2) continue;
                addWord(strings[0].trim(), strings[1].trim());
            }
        } finally {
            reader.close();
        }
        mLoaded = true;
    }

public synchronized List<Word> getAllMatches(Resources resources) throws IOException {
        List<Word> list = new ArrayList<Word>();
        InputStream inputStream = resources.openRawResource(R.raw.definitions);
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        try {
            String line;
            while((line = reader.readLine()) != null) {
                String[] strings = TextUtils.split(line, ":");
                if (strings.length < 2) continue;
                Word word = new Word(strings[0].trim(), strings[1].trim());
                list.add(word);
            }
        } finally {
            reader.close();
        }

        return list;
    }

One could use the gzip single file compression ("big-text.txt.gz"), and use GZipInputStream.

The same String should be kept once in memory. In needed, before passing a string on, you could search it:

Map<String, String> sharedStrings = new HashMap<>();

String share(String s) {
    String sToo = sharedStrings.get(s);
    if (sToo == null) {
        sToo = s;
        sharedStrings.put(s, s);
    }
    return sToo;
}

The suggestion to use a database is a good one too.

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