字节流、字符流、缓冲流

本文深入讲解Java中的IO流概念,包括字节流与字符流的使用,如FileInputStream、FileOutputStream、FileReader、FileWriter等,并通过实例演示了文件复制与读写操作,同时介绍了缓冲流的使用及性能提升。

在这里插入图片描述
其实内容比较简单,就是比较杂而已。本来还想总结一下,觉得总结不出来,直接上图吧,觉得还是比较清楚的。
先介绍一些概念:
1.输入流:Input 硬盘读取到内存
输出流: output 内存写出到硬盘
2.IO流整体分为四种八个类

字节输入流InputStream(抽象父类fileInputStream(子类)
字节输出流"OutputStream(抽象父类)类fileOutputStream(子类)
字节输出流Reader(抽象父类)类fileReader(子类)
字节输出流Writer(抽象父类)类fileWriter(子类)

3.在java中字节流可以操作任意类型的文件,例如:.txt
4.在java中字符流只能操作文本文件(文本文件就是可以用系统自带的txt编辑器打开并且不乱码的文件)。

1)FileOutputStream的使用

FileoutputStream可以一个字节一个字节的写,也可以一个字节数组的写。

public class Demo01 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("a.txt");
        //一个字节一个字节的写
        fos.write(97);
        FileOutputStream fos2 = new FileOutputStream("b.txt");
        //字符数组写出
        fos.write("i love java".getBytes());
    }
}    

2)FileInputStream的使用

public class Demo04 {
    public static void main(String[] args) throws IOException {
        FileInputStream fi = new FileInputStream("a.txt");
        int len=-1;
        //一个字节一个字节的读取
        /* while ((x=fi.read())!=-1){
            System.out.println((char) x);
        }*/
        //按字节数组的读取
        byte[] buf = new byte[1024];
        while ((len=fi.read(buf))!=-1){
            System.out.println(new String(buf,0,len));
        }
    }
}

需求:利用字节流将E盘下的a.jpg复制到D盘下

public class Demo06 {
    public static void main(String[] args) throws IOException {
        FileInputStream fi = new FileInputStream("E:\\a.jpg");
        FileOutputStream fo= new FileOutputStream("D:\\a.jpg");
        int len =-1;
        while((len = fi.read())!=-1){
            fo.write(len);
        }
    }
}

3)FileWriter的使用

同上,也有两种,可以按字节来写出,也可以按字节数组写出

public class FileWriterDemo4 {
	public static void main(String[] args) throws IOException {
		//创建输出流对象
		//FileWriter fw = new FileWriter("day09\\4.txt");
		FileWriter fw = new FileWriter("day09\\4.txt",true); //表示追加写入,默认是false
		
		for(int x=0; x<10; x++) {
			fw.write("hello"+x+"\r\n");
		}
		//释放资源
		fw.close();//刷新与关闭 fw.flush();不刷新关闭,不能成功写入
	}
}

4)FileReader的使用

同上,也有两种,可以按字节来读取,也可以按字节数组读取

public class FISRead {
    public static void main(String[] args) throws IOException {
      	// 使用文件名称创建流对象
       	FileReader fr = new FileReader("D:\\out.txt");
      	// 定义变量,保存有效字符个数
        int len =0;
        // 定义字符数组,作为装字符数据的容器
        char[] cbuf = new char[1024];
        // 循环读取
        while ((len = fr.read(cbuf))!=-1) {
            System.out.println(new String(cbuf,0,len));
        }
    	// 关闭资源
        fr.close();
    }
}

来个测试题,使用字符流复制文件

//需求:将1.txt赋值到a.txt
public class TextCopyDemo {
    public static void main(String[] args) {
        //定义一个字符输出流以及字符输入流对象,jdk引入的自动关闭流
        try (F`ileReader fr = new FileReader("1.txt");`
             FileWriter fw = new FileWriter("copy.txt");) {
            int x = -1;
            char[] ch = new char[1024];
            while ((x = fr.read(ch)) != -1) {
                fw.write(ch, 0, x);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

说明:这里的IO异常采取是捕获的方式处理的,try(…;…)是从jdk1.7才开始使用的,具有自动关闭流的功能

缓冲流

1)字节缓冲流

public class BufferedDemo {
    public static void main(String[] args) throws FileNotFoundException {
      	// 记录开始时间
        long start = System.currentTimeMillis();
		// 创建流对象
        try (
			BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\test\\2.mp4"));
		 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("F:\\2.mp4"));
        ){
          	// 读写数据
            int len;
            byte[] bytes = new byte[8*1024];
            while ((len = bis.read(bytes)) != -1) {
                bos.write(bytes, 0 , len);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
		// 记录结束时间
        long end = System.currentTimeMillis();
        System.out.println("缓冲流使用数组复制时间:"+(end - start)+" 毫秒");
    }
}

2)字符缓冲流

这有个readline方法注意一下,特有方法。

public class BufferedReaderDemo02 {
    public static void main(String[] args) throws  IOException {
        BufferedReader br = new BufferedReader(new FileReader("a.txt"));
        String line=null;
        while ((line = br.readLine())!=null){
            System.out.println(line);
            System.out.println("-------------");
        }
        br.close();
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值