【问题标题】:How to read .txt file line by line from assets folder efficiently & in a fast way in Android Studio?如何在 Android Studio 中高效快速地从 assets 文件夹中逐行读取 .text 文件?
【发布时间】:2018-11-18 23:19:55
【问题描述】:

我的资产文件夹中有一个文本文件,其中大约有 15000 行,我想遍历每一行,我使用 BufferedReader 应用了逻辑,但是遍历每一行需要很长时间(1 分钟+) .

现在我需要减少阅读每一行的时间以最大化用户体验。

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;
        }
    };

【问题讨论】:

  • 字符串去哪儿了,是数据库表还是内存类?每次游戏冷启动时都会发生这种情况吗?
  • 房间数据库,但我真的不认为这是原因,因为我尝试删除此行并重复该过程,它花了大约同一时间
  • 如果@kiskae 回答不能解决您的问题:我建议Shipping an application with a pre-populated database,但看起来Room 并不特别支持这种行为:Room library can copy db from asset folder?
  • publishProgress(percentage) 的代码是什么以及您更新 UI 的频率(即 15000 次更新没有用,使 UI 更新有条件)
  • publishProgress 触发 onProgressUpdate,它恰好更新我的百分比以通知用户。根据文档:“onProgressUpdate(Progress...),在调用 publishProgress(Progress...) 后在 UI 线程上调用。执行的时间未定义。此方法用于显示任何形式的进度后台计算仍在执行时的用户界面。例如,它可用于动画进度条或在文本字段中显示日志。"

标签: java android bufferedreader


【解决方案1】:

在不知道执行addGame 需要多长时间的情况下,很难确定,但我怀疑问题在于您对runOnUiThread 的使用。您说您的文件包含 15000 行,因此它将创建并安排 UI 处理程序必须按照您设置的方式为每个项目运行的更新。

你不知道的是AsyncTask 有一个内置的方法来处理你的请求去抖动。查看处理 UI 更新的 publishProgressonProgressUpdate 方法。

【讨论】:

  • 我已经用 onProgressUpdate 尝试过你的建议,但它并没有解决我的时间问题,更新了帖子中的代码。
  • 好吧,确实,你调用 UI 15000 次是正确的,这很糟糕并且会浪费时间,我已经通过提供 switch-case 解决了这个问题,它减少了时间,但那是'不是唯一的问题,我的房间数据库有问题,因为插入游戏需要很长时间(我尝试将它存储在数组列表而不是房间中,它要快得多 - 3 或 4 秒),所以基本上房间和调用UI 15k 次是问题,你和@Morrison 都发现了 room & runOnUiThread 的问题,谢谢你的帮助。 :) 你们俩都应该得到绿色箭头
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多