18.2 IO流之字节流和字节缓冲流

IO流介绍:

IO:输入/输出(Input/Output)
流:是一种抽象概念,是对数据传输的总称。也就是说数据在设备间的传输称为流,流的本质是数据传 输
IO流就是用来处理设备间数据传输问题的。常见的应用:文件复制;文件上传;文件下载

IO流的分类:

按数据的流向分为:

  1. 输入流:读数据
  2. 输出流:写数据

按数据类型分为:

字节流:

  1. 字节输入流
  2. 字节输出流

字符流:

  1. 字符输入流
  2. 字符输出流

 字节流写数据

使用字节输出流写数据的步骤:

创建字节输出流对象(调用系统功能创建了文件,创建字节输出流对象,让字节输出流对象指向文件)
调用字节输出流对象的写数据方法
释放资源(关闭此文件输出流并释放与此流相关联的任何系统资源)

写数据的方法:

实例代码:

public class OutPutStreamDemo {
    public static void main(String[] args) throws IOException {
        File f = new File("day08\\java.txt");
        //创建文件
        f.createNewFile();

        //创建字节输出流对象(如果文件不存在则自动创建,创建后再写入)
        FileOutputStream fos = new FileOutputStream("day08\\java.txt");

        //void write(int b):将指定的字节写入此文件输出流
        fos.write(97);
        fos.write(98);
        fos.write(99);
        fos.write(48);
        fos.write(65);

        //void close():关闭此文件输出流并释放与此流相关联的任何系统资源
        fos.close();

        //创建字节数组
        byte[] bytes = {55,55,49,54,51,55,54,48,48,64,113,113,46,67,79,77};
        //将字节数组传入
        FileOutputStream fos2 = new FileOutputStream("day08\\java2.txt");
        fos2.write(bytes,0,bytes.length);

    }
}

运行结果:

字节流写数据的两个问题:

字节流写数据如何实现换行:

windows:\r\n
linux:\n
mac:\r

实例代码:

public class OutPutStreamDemo_2 {
    public static void main(String[] args) throws IOException {
        //构造方法append默认为false,改为true后可以追加
        FileOutputStream fos = new FileOutputStream("day08\\java3.txt",true);

        //写入
        for (int i = 0; i < 10; i++) {
            //string的etBytes()可以将字符串转化为字节数组
            fos.write("Hello".getBytes());
            //winds换行符\r\n
            fos.write("\r\n".getBytes());
        }
        fos.close();
    }
}

运行结果:

字节流写数据加异常处理

实例代码:

public class OutPutStreamDemo_3 {
    public static void main(String[] args) {
        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream("day08\\java4.txt");
            fos.write("hello".getBytes());
            fos.write("world".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

运行结果:

 

字节流读数据(一次读一个字节和一次读一个字节数组数据)

FileInputStream(String name):通过打开与实际文件的连接来创建一个FileInputStream ,该文件由文 件系统中的路径名name命名 

字节输入流读取数据的步骤 :

创建字节输入流对象
调用字节输入流对象的读数据方法
释放资源

实例代码:

public class InPutStreamDemo {
    public static void main(String[] args) throws IOException {
        //创建输入流对象
        FileInputStream fis = new FileInputStream("day08\\java4.txt");
        int by;
        //读文件  如果出去来的值不是-1则说明还有数据 继续读    read不带参数时是一个字节一个字节的读,返回值是读取的数据
        while ((by = fis.read()) != -1){
            System.out.print(by);
            System.out.println((char)by);
        }

        fis.close();
    }
}

运行结果:单字节读出来的字符会变成相应的ASCII码

public class InPutStreamDemo {
    public static void main(String[] args) throws IOException {
        //创建输入流对象
        FileInputStream fis = new FileInputStream("day08\\java4.txt");

        byte[] bytes = new byte[1024];
        int len;
        //read传入参数的类型为字节数组fis.read(bytes) 含义是:一次读取by.length 的数据
        //这时候read带参数 fis.read(by) 返回的是实际读取数据的长度 ,读取的数据存到了bytes字节数组中
        while ((len = fis.read(bytes)) != -1){
            System.out.println(new String(bytes,0,len));
            System.out.println(len);
        }

        fis.close();
    }
}

运行结果:

 

字节流复制文本文件

实例代码:

public class CopyFile {
    public static void main(String[] args) throws IOException {
        //复制文件, 复制的对象就是 读的文件
        FileInputStream fis = new FileInputStream("E:\\java55\\day03\\SwitchDemo.java");
        //复制后的文件   写
        FileOutputStream fos = new FileOutputStream("day08\\java5.txt");

        //复制
        int by;
        while ((by = fis.read()) != -1){
            fos.write(by);
        }
        fis.close();
        fos.close();
    }
}

运行结果:

 

字节流复制图片

实例代码:

public class CopyPicture {
    public static void main(String[] args) throws IOException {
        //读
        FileInputStream fis = new FileInputStream("C:\\Users\\li771637600\\Pictures\\Saved Pictures\\20151117170929141600.jpg");
        //写
        FileOutputStream fos = new FileOutputStream("day08\\xk.jpg");

        byte[] by = new byte[1024];
        int len;
        while ((len = fis.read(by)) != -1){
            fos.write(by,0,len);
        }
        fis.close();
        fos.close();
    }
}

运行结果:

 

字节缓冲流介绍 

BufferOutputStream:该类实现缓冲输出流。 通过设置这样的输出流,应用程序可以向底层输出流写 入字节,而不必为写入的每个字节导致底层系统的调用 
BufferedInputStream:创建BufferedInputStream将创建一个内部缓冲区数组。 当从流中读取或跳过 字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次很多字节

构造方法:

复制视频代码演示:

public class BufferStream_2 {
    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        copy2();
        long end = System.currentTimeMillis();
        System.out.println(end - start);
    }
    
    public static void copy2() throws IOException{
        
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\BaiduNetdiskDownload\\day01\\day01\\day01_video\\day01_03(基础语法)java语言开发环境JDK.avi"));
        
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day09\\jreAndjdk.avi"));

        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1){
            bos.write(bytes,0,len);
        }

        bis.close();
        bos.close();
    }
}

运行结果:

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值