Android Reading a large text efficiently in Java

泪湿孤枕 提交于 2020-02-25 04:13:28

问题


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:


  1. Use StringBuilder instead of StringBuffer.

    StringBuffer is synchronized, and you don't need that.

  2. Don't use String.valueOf, which builds a string, negating the value using a 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.

    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())).

    Although, as blackapps points out in a comment, you could simply check if strLine2 contains that string.

  3. Don't use string concatenation inside a call to append: make two separate calls.

    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.




回答2:


My new Created code:(My test device is a Samsung 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();
                }
            }

        }


来源:https://stackoverflow.com/questions/58564335/android-reading-a-large-text-efficiently-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!