Java test byte stream and character stream, and the character stream with buffer read speed comparison

The test file is a txt format file with a size of 2.12MB

package IO;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;

    /**
     * 我创建了两个方法,
     * fileReader()用来测试字节流的读取速度
     * fileInputStream()用来测试字符流读取速度
     * 测试文件为 0.98G的test.txt文本文件
     * @author liqifeng
     *
     */
public class speedTest {
    
    

    //文件类
    static File file;

    /**
     * 此方法用来测试带缓存的字符流的读取速度
     * @param file
     * @throws Exception
     */
    public static void bufferReader(File file) throws Exception{
        FileReader fileReader=new FileReader(file);
        BufferedReader bufferedReader=new BufferedReader(fileReader);
        while(true){
            bufferedReader.readLine();
            if(bufferedReader.readLine()==null){
                break;
            }
        }
    }

    /**
     * 此方法用来测试字符流的读取速度
     * @param file
     * @throws Exception
     */

    public static void  fileReader(File file) throws Exception{
        FileReader f=new FileReader(file);
        while (true) {
            f.read();
            if(f.read()==-1){
                break;
            }
        }

        f.close();

    }

    /**
     * 此方法用来测试字节流的读取速度
     * @param file
     * @throws Exception
     */
    public static void fileInputStream( File file) throws Exception{
        FileInputStream f=new FileInputStream(file);

        while(true){
            f.read();
            if(f.read()==-1){
                break;
            }
        }
        f.close();
    }
    public static void main(String[] args) throws Exception {
        //初始化文件
        file=new File("E:/java/test.txt");
        System.out.println("字符流运行速度测试如下(五次)");
        for(int i=0;i<5;i++){

            //开始的时间戳
            long before=System.currentTimeMillis(); 

            //运行程序
            fileReader(file);

            //结束的时间戳
            long after=System.currentTimeMillis(); 

            //总用时
            long result=after-before;

            System.out.println(result);
        }
        System.out.println();

        System.out.println("字节流运行速度测试如下(五次)");
        for(int i=0;i<5;i++){
            long before=System.currentTimeMillis(); 
            fileInputStream(file);
            long after=System.currentTimeMillis(); 
            long result=after-before;
            System.out.println(result);
        }
        System.out.println();

        System.out.println("带缓存的字符流运行速度测试如下(五次)");
        for(int i=0;i<5;i++){
            long before=System.currentTimeMillis(); 
            bufferReader(file);
            long after=System.currentTimeMillis(); 
            long result=after-before;
            System.out.println(result);
        }
    }
}

The results of the operation are as follows

Write picture description here

It can be clearly seen that in this test, the character stream reading speed is faster than the byte stream reading speed, and the character stream with buffer is faster than the character stream without buffer.

Guess you like

Origin blog.csdn.net/mrliqifeng/article/details/78073356