文件写入时使用缓冲区与不使用缓冲区效率提升3倍测试

//不使用缓冲区,用时100MS
    public static void main(String[] args) {
        Writer writer = null;
        try {
            Long startTime = System.currentTimeMillis();
            writer = new FileWriter("D:\\zp_project\\rt.txt");
            for (int i = 0; i <500000 ; i++) {
                writer.write(i);
            }
            Long endTime = System.currentTimeMillis();
            System.out.println(endTime-startTime);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }


//使用缓冲区 用时33MS
    public static void main(String[] args) {
        Writer writer = null;
        BufferedWriter bufferedWriter = null;

        try {
            Long start = System.currentTimeMillis();
            writer = new FileWriter("D:\\zp_project\\bf.txt");
            bufferedWriter = new BufferedWriter(writer);
            for (int i = 0; i <500000 ; i++) {
                bufferedWriter.write(i);
            }
            Long end = System.currentTimeMillis();
            System.out.println(end - start);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bufferedWriter.close();
                writer.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

 

上一篇:“21天好习惯”第一期-5


下一篇:单个json文件转换成csv文件