缓冲流..

本文介绍了Java中的字节缓冲输入流和输出流(如`BufferedInputStream`和`BufferedOutputStream`),展示了如何通过它们提高文件操作的效率,并通过实例演示了`BufferedReader`和`BufferedWriter`在字符缓冲流中的应用。同时,还对比了不同数据拷贝方法的性能差异。
摘要由CSDN通过智能技术生成
* 缓冲流:提高读取、写入的效率
*
* 字节缓冲输出流:
*      BufferedOutputStream
* 字节缓冲输入流:
*      BufferedInputStream
package MONA.demo06_字节缓冲流;

import java.io.BufferedOutputStream;
import java.io.FileOutputStream;

public class Demo {
    public static void main(String[] args) throws Exception {
        //FileOutputStream fos = new FileOutputStream("3.txt");
        //缓冲流
        //第一种写法
//        BufferedOutputStream bos = new BufferedOutputStream(fos);
//        bos.write(97);
//        bos.close();
//        fos.close();
//        System.out.println("转换完成");

        //第二种写法:FileOutputStream匿名对象
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("4.txt"));
        bos.write(97);
        bos.close();
    }
}

字节缓冲输入流

import java.io.FileInputStream;

/**
 * 字节缓冲输入流
 *      BufferedInputStream
 */
public class Demo02 {
    public static void main(String[] args) throws Exception {
//        FileInputStream fis = new FileInputStream("test.txt");
//        BufferedInputStream bis = new BufferedInputStream(fis);
//        System.out.println((char)bis.read());

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("test.txt"));
        System.out.println((char)bis.read());
    }
}

字符缓冲输入流

package MONA.demo07_字符缓冲流;

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;

/**
 * 字符缓冲输入流(缓冲字符输入流)
 *      BufferedReader
 */
public class Demo01 {
    public static void main(String[] args) throws Exception {
        FileOutputStream fos = new FileOutputStream("1.txt");
        fos.write(97);
        FileReader fr = new FileReader("1.txt");
        BufferedReader br = new BufferedReader(fr);
        int c = br.read();
        System.out.println((char) c);
    }
}

字符缓冲输出流

package MONA.demo07_字符缓冲流;

import java.io.*;

/**
 * 字符缓冲输出流
 *      BufferedWriter
 */
public class Demo02 {
    public static void main(String[] args) throws Exception {
        FileWriter fw = new FileWriter("2.txt");
        BufferedWriter bw = new BufferedWriter(fw);
        bw.write("你好美女1");
        //换行: 之前是/r/n   现在newLine()
        bw.newLine();
        bw.write("你好美女2");
        bw.flush();
        bw.close();

        Reader fr = new FileReader("2.txt");
        BufferedReader br = new BufferedReader(fr);
        //缓冲流提供的方法,读取一行
        String line = br.readLine();
        System.out.println(line);
    }
}

IO流拷贝数据效率对比

package MONA.demo09_缓冲流拷贝文件;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;

public class Demo01 {
    public static void main(String[] args) throws Exception {
        m1();
        m2();
        m3();
        m4();
    }
    //缓冲流:一次读取一个字符
    public static void m1() throws Exception {
        long start = System.currentTimeMillis();
        FileReader fr = new FileReader("1.txt");
        BufferedReader br = new BufferedReader(fr);
        BufferedWriter bw = new BufferedWriter(new FileWriter("2.txt"));
        int c = 0;
        while ((c = br.read()) !=-1){
            bw.write(c);
        }
        bw.close();
        br.close();
        fr.close();
        long end = System.currentTimeMillis();
        System.out.println("使用缓冲流复制完成"+((end - start)));
    }
    //默认方式:一次读取一个字符
    public static void m2() throws Exception {
        long start = System.currentTimeMillis();
        FileReader fr = new FileReader("1.txt");
        FileWriter bw = new FileWriter("3.txt");
        int c = 0;
        while ((c = fr.read()) !=-1){
            bw.write(c);
        }
        bw.close();
        fr.close();
        long end = System.currentTimeMillis();
        System.out.println("不使用缓冲流复制完成"+((end - start)));
    }
    //缓冲流:一次读取一个字符
    public static void m3() throws Exception {
        long start = System.currentTimeMillis();
        FileReader fr = new FileReader("1.txt");
        BufferedReader br = new BufferedReader(fr);
        BufferedWriter bw = new BufferedWriter(new FileWriter("45.txt"));
        char[] chars = new char[1024];
        int count = 0;
        while ((count = br.read(chars)) !=-1){
            bw.write(chars,0,count);
        }
        bw.close();
        br.close();
        fr.close();
        long end = System.currentTimeMillis();
        System.out.println("使用缓冲流:字符数组"+((end - start)));
    }
    //不缓冲流:一次读取一个字符
    public static void m4() throws Exception {
        long start = System.currentTimeMillis();
        FileReader fr = new FileReader("1.txt");
        FileWriter bw = new FileWriter("45.txt");
        char[] chars = new char[1024];
        int count = 0;
        while ((count = fr.read(chars)) !=-1){
            bw.write(chars,0,count);
        }
        bw.close();
        fr.close();
        long end = System.currentTimeMillis();
        System.out.println("不使用缓冲流:字符数组"+((end - start)));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值