字节流介绍

一、字节流顶层的抽象

         InputStream和OutputStream专门用来操作字节流,但这两个是抽象基类,不能用来实例化对象,FileInputStream extends InputStream并且FileOutPutStream  extends OutputStream,所以我们用FileInputStream和FileOutputStream来实例化对象。

二、核心方法介绍

<一>InputStream下的方法

1.int read();//每次读取一个字节的数据,读取到文件末尾返回-1.

比如:hello.tx文件里面内容为“abcde”,运行结果如下

FileInputStream inputStream = new FileInputStream("D:\\untitled8\\src\\com\\src1\\hello.tx");
        int read = inputStream.read();
        System.out.println(read);

运行结果:

2.int read(byte[] b);//每次读取数据放到bytes数组,返回读取到数据的有效个数,

读取到文件末尾返回-1.

FileInputStream inputStream = new FileInputStream("D:\\untitled8\\src\\com\\src1\\hello.tx");
        byte[] bytes = new byte[10];
        int read1 = inputStream.read(bytes);
        System.out.println(read1);

运行结果:

3.int read(byte[] b,int off,int len);//每次读到的数据放到bytes数组中,从数组中的off位置开始,放len个长度

FileInputStream inputStream = new FileInputStream("D:\\untitled8\\src\\com\\src1\\hello.tx");
        byte[] bytes = new byte[10];
        int read2 = inputStream.read(bytes, 3, 5);
        System.out.println(read2);
        System.out.println(new String(bytes));

运行结果:

<二>OuttputStream下的方法

1. void write();//将int值写入到输出流,int转换为字符型

FileOutputStream outputStream = new FileOutputStream("D:\\untitled8\\src\\com\\src1\\hello.tx");
        outputStream.write(97);

运行结果:

之前文件hello.tx中为abcde,写了之后为a

2.void write(byte[] b);//将字节数组内容写入到输出流,即文件(内存)

FileOutputStream outputStream = new FileOutputStream("D:\\untitled8\\src\\com\\src1\\hello.tx");
        outputStream.write("212".getBytes());

运行结果:

之前hello.tx文件中为a,写完之后为212

3.void write(byte[] b,int off,int len);//将字节数组中内容,从off位置开始长度为len,写入到输出流,即文件中。

FileOutputStream outputStream = new FileOutputStream("D:\\untitled8\\src\\com\\src1\\hello.tx");
        outputStream.write("211abcde".getBytes(),3,4);

运行结果:

之前hello.tx文件中为212,写完之后为abcd。

三、文件输出流和文件输入流

<一>文件输入流   FileInputStream

FileInputStream fileInputStream = new FileInputStream("D:\\\\untitled8\\\\src\\\\com\\\\src1\\\\hello.tx");
        int i;
        byte[] bytes = new byte[2];
        while ((i = fileInputStream.read(bytes)) != -1){
            System.out.println(new String(bytes));
        }

运行结果:

         原文件中内容为abcde,但输出为图示结果,因为每次读取会覆盖上一次读取到的byte数组,最后一次不能整数形式读取,就会造成一部分数据未被覆盖,而产生脏数据。

解决措施:

System.out.println(new String(bytes,0,i));

运行结果:

<二>文件输出流 FileOutputStream

构造方法:

1.FileOutputStream(String name);//传入一个路径

2.FileOutputStream(String name,boolean append);//append默认为false,覆盖内容,true是追加内容

3.FileOutputStream(File name);//传入一个File类型的实例

4.FileOutputStream(File name,boolean append);

FileOutputStream outputStream  = new FileOutputStream("D:\\untitled8\\src\\com\\src1\\hello.tx",true);
        outputStream.write("212".getBytes());

运行结果:

之前文件内容为212abcde,执行之后为212abcde212,追加,为true的情况

File file = new File("D:\\untitled8\\src\\com\\src1\\hello.tx");
        FileOutputStream outputStream  = new FileOutputStream("D:\\untitled8\\src\\com\\src1\\hello.tx",false);
        outputStream.write("212".getBytes());

运行结果:

之前文件内容为212abcde212,执行之后为212,覆盖掉之前的,append为false的情况

 

 

 

 

 

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值