简体   繁体   English

Android 高效读取大文本 Java

[英]Android Reading a large text efficiently in Java

My code is too slow我的代码太慢了

How can I make my code efficiently?如何有效地编写代码? Currently the code needs several minutes until the file was read, which is way too long.目前代码需要几分钟才能读取文件,这太长了。 Can this be done faster?这可以更快地完成吗? There is no stacktrace, because it works, but too slow.没有堆栈跟踪,因为它可以工作,但是太慢了。 Thanks!谢谢!

The Problem Code:问题代码:

private void list(){
        String strLine2="";
        wwwdf2 = new StringBuffer();

        InputStream fis2 = this.getResources().openRawResource(R.raw.list);
        BufferedReader br2 = new BufferedReader(new InputStreamReader(fis2));
        if(fis2 != null) {
            try {
                LineNumberReader lnr = new LineNumberReader(br2);
                String linenumber = String.valueOf(lnr);
                int i=0;
                while (i!=1) {
                    strLine2 = br2.readLine();
                    wwwdf2.append(strLine2 + "\n");
                    String contains = String.valueOf(wwwdf2);
                    if(contains.contains("itisdonecomplet")){
                       i++;
                    }
                }
              //  Toast.makeText(getApplicationContext(), strLine2, Toast.LENGTH_LONG).show();
                Toast.makeText(getApplicationContext(), wwwdf2, Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        
    }
  1. Use StringBuilder instead of StringBuffer .使用StringBuilder而不是StringBuffer

    StringBuffer is synchronized, and you don't need that. StringBuffer是同步的,你不需要它。

  2. Don't use String.valueOf , which builds a string, negating the value using a StringBuffer / Builder .不要使用String.valueOf ,它构建一个字符串,使用StringBuffer / Builder否定该值。 You are building a string from the whole buffer, checking it, discarding the string, then constructing nearly the same string again.您正在从整个缓冲区构建一个字符串,检查它,丢弃该字符串,然后再次构建几乎相同的字符串。

    Use if (wwwdf2.indexOf("itisdonecomplet") >= 0) instead, which avoids creating the string.改用if (wwwdf2.indexOf("itisdonecomplet") >= 0) ,这样可以避免创建字符串。

    But this will still be reasonably slow, as although you would not be constructing a string and searching through it all, you are still doing the searching.但这仍然会相当慢,因为尽管您不会构建字符串并搜索所有内容,但您仍在进行搜索。

    You can make this a lot faster by only searching the very end of the string.您可以通过仅搜索字符串的末尾来加快速度。 For example, you could use wwwdf2.indexOf("itisdonecomplet", Math.max(0, wwwdf2.length() - strLine2.length() - "itisdonecomplet".length())) .例如,您可以使用wwwdf2.indexOf("itisdonecomplet", Math.max(0, wwwdf2.length() - strLine2.length() - "itisdonecomplet".length()))

    Although, as blackapps points out in a comment, you could simply check if strLine2 contains that string.尽管正如 blackapps 在评论中指出的那样,您可以简单地检查strLine2是否包含该字符串。

  3. Don't use string concatenation inside a call to append : make two separate calls.不要在对append的调用中使用字符串连接:进行两个单独的调用。

     wwwdf2.append(strLine2); wwwdf2.append("\n");
  4. You don't check if you reach the end of the file.您不检查是否到达文件末尾。 Check if strLine2 is null, and break the loop if it is.检查strLine2是否为null,如果是则断开循环。

My new Created code:(My test device is a Samsung S8)我的新创建代码:(我的测试设备是三星 S8)

 private void list(){
            String strLine2="";
            wwwdf2 = new StringBuilder();

            InputStream fis2 = this.getResources().openRawResource(R.raw.list);
            BufferedReader br2 = new BufferedReader(new InputStreamReader(fis2));
            if(fis2 != null) {
                try {
                    LineNumberReader lnr = new LineNumberReader(br2);
                    String linenumber = String.valueOf(lnr);
    int i=0;
                    while (i!=1) {
                        strLine2 = br2.readLine();
                        wwwdf2.append(strLine2);
                        wwwdf2.append("\n");
                        if (wwwdf2.indexOf("itisdonecomplet") >= 0){
                            i++;
                        }


                    }
                  //  Toast.makeText(getApplicationContext(), strLine2, Toast.LENGTH_LONG).show();
                    Toast.makeText(getApplicationContext(), wwwdf2, Toast.LENGTH_LONG).show();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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