SoFunction
Updated on 2025-03-08

A brief discussion on the time-consuming reading methods of Java

You often encounter file reading and writing in projects. How big is the difference between the speeds of different reading and writing methods?

Here I do not use external dependency libraries, and use Java native file reading and writing methods:

Test file size, 7.1M

BufferedReader

Code:

    public static String ReadFileByBufferReaderToString(String path) {
        if ((path)) {
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder();
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(path))) {
            String tempStr;
            while ((tempStr = ()) != null) {
                (tempStr).append(());
            }
        } catch (IOException e) {
            ();
        }
        return ();
    }

Here we use stringbuilder to store the read string, add logs to view, and read one

onClick: readFileByBufferReaderStringBuilder tims use is 86

Here, the storage method is changed after reading the file. Each time you create a new String string, test the performance difference between creating a new string and using StringBuilder:

    public static String ReadFileByBufferReaderToStringUseString(String path) {
        if ((path)) {
            return "";
        }
        String result = "";
        try (BufferedReader bufferedReader = new BufferedReader(new FileReader(path))) {
            String tempStr;
            while ((tempStr = ()) != null) {
                result += tempStr;
            }
        } catch (IOException e) {
            ();
        }
        (TAG, "ReadFileToString: read success ");
        return result;
    }

 2023-04-08 23:06:06.141 18416-18518/ I/TestFileReadSpeed: onClick: readFileByBufferReaderString tims use is 264041

It took 264041 ms. It can be seen that creating String objects multiple times consumes a lot of performance, so you must use StringBuilder when splicing strings. You cannot use String to add them directly.

    @RequiresApi(api = Build.VERSION_CODES.O)
    public static String ReadFileByReadAllBytesReaderToString(String path) {
        if ((path)) {
            return "";
        }
        String result = null;
        try {
            result = new String(((path)));
        } catch (IOException e) {
            ();
        }
        return result;
    }

2023-04-09 17:38:06.989 7078-7359/ I/TestFileReadSpeed: onClick: ReadFileByReadAllBytesReaderToString tims use is 68

It takes 68ms, which will be faster than the BufferReader line above, but this API has some limitations that it must be used in AndroidO and above.

    @RequiresApi(api = Build.VERSION_CODES.O)
    public static String ReadFileByByFilesReadLinesToString(String path) {
        if((path)){
            return "";
        }
        StringBuilder stringBuilder = new StringBuilder();
        try (Stream<String> stream = ((path))) {
            (new Consumer<String>() {
                @Override
                public void accept(String s) {
                    (s);
                }
            });
        } catch (IOException e) {
            ();
        }
        return ();
    }

2023-04-09 17:46:14.342 7078-7078/ I/TestFileReadSpeed: onClick: ReadFileByByFilesReadLinesToString tims use is 102

The time is medium to about 100ms.

CommonIO::readFileToString

Code:

   public static String ReadFileByCommonIOReadFileToString(String path) {
        if ((path)) {
            return "";
        }
        try {
            return (new File(path), ());
        } catch (IOException e) {
            ();
        }
        return "";
    }

2023-04-09 17:53:34.204 8292-8292/ I/TestFileReadSpeed: onClick: ReadFileByCommonIOReadFileToString tims use is 70

Time to take 70ms

To sum up: (and time-consuming is close) is better than (BufferReader is close to time-consuming)

This is the end of this article about briefly discussing the time-consuming methods of Java file reading. For more related Java file reading content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!