简体   繁体   中英

How to read .txt file line by line from assets folder efficiently & in a fast way in Android Studio?

I've a text file in my assets folder, where it has around 15000 lines, I wanna loop through each line, I've applied the logic using BufferedReader, but it takes too long to loop through each line(1min+).

Now I need to reduce the time of reading each line to maximize the UX.

AsyncTask<Void, String, Void> asyncTask = new AsyncTask<Void, String, Void>() {
        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            appBarLayout.setVisibility(View.VISIBLE);
            viewpager.setVisibility(View.VISIBLE);
            splashScreen.setVisibility(View.GONE);
            gamesLoaded = true;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            makeRandomText(gameRandomText, "Random Text" + new Random().nextInt(1000));
        }
        @Override
        protected void onProgressUpdate(String... values) {
            super.onProgressUpdate(values);
            gamePercentage.setText(values[0]);
        }
        @Override
        protected Void doInBackground(Void... voids) {
            BufferedReader reader = null;
            try {
                reader = new BufferedReader(
                        new InputStreamReader(getAssets().open("games.txt"), "UTF-8"));

                int startingCount = 0;
                // do reading, usually loop until end of file reading
                String mLine;
                while ((mLine = reader.readLine()) != null) {
                    Game gameAdded = new Game(mLine);
                    addGame(database, gameAdded);
                    startingCount++;
                    String percentage = startingCount * 100 / Constants.GAMES_COUNT + "%";
                    publishProgress(percentage);
                }
            } catch (IOException e) {
                //log the exception
            } finally {
                if (reader != null) {
                    try {
                        reader.close();
                    } catch (IOException e) {
                        //log the exception
                    }
                }
            }
            return null;
        }
    };

我的资产文件夹

Without knowing how long it takes to execute addGame it is hard to say for sure, but I suspect the issue lies with your use of runOnUiThread . You said your file contains 15000 lines so it will create and schedule an update that the UI handler HAS to run for each item with the way you have it set up.

What you are not aware of is that AsyncTask has a built-in way to handle progress which handles request debouncing for you. Look into the publishProgress and onProgressUpdate methods to handle to UI updates.

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