字节流
字节流
字节流的概述和分类
IO流概述:
- IO:输入/输出(Input/Output)
 - 流:
- 是一种抽象概念,是对象传输的总称。
 - 也就是说数据在设备间的传输称为流。
 - 本质是数据传输
 
 - IO流就是用来设备间数据传输问题的
- 常见应用:文件复制、文件上传、文件下载
 
 
IO流分类:
- 按照数据流向:
- 输入流:读数据
 - 输出流:写数据
 
 - 按照数据类型来分
- 字节流
- 字节输入流;字节输出流
 
 - 字符流
- 字符输入流;字符输出流
 
 
 - 字节流
 - 一般按照数据类型来分
 - 使用场景:
- 能用记事本打开且能读懂,就使用字符流;
 - 否则使用字节流(万能)。
 
 
字节流
1.字节流抽象基类
- InputStream:这个抽象类是表示字节输入流的所有类的超类
 - OutputStream:这个抽象类是表示字节输出流的所有类的超类
 - 子类名特点:以其父类名作为其子类名的后缀
 
 FileOutputStream:文件输出流用于将数据写入File
- FileOutputStream(String name):
- 创建文件输出流以指定的名称写入文件
 
 
2.步骤:
IO流写入数据:
    1.写对象
        (1).调用系统功能创建了文件
        (2).创建了字节输出流对象
        (3).让字节输出流指向创建好的文件
    2.调用字节输出流对象写数据
    3.释放资源
3.字节流写数据的三种方式
(1)void write(int b)
 将指定的字节写入此文件输出流,一次写一个字节数据
(2)void write(byte[] b)
 将b.length字节从指定的字节数组写入此文件输出流
 一次写一个字节数组数据
(3)void write(byte[] b,int off,int len)
 将len字节从指定的字节数组开始,从偏移量off(开始索引位置)开始写入此文件输出流
 一次写一个字节数组的部分数据
4.字节流写数据的两个小问题
- 
字节流如何实现换行:
windows:\r\n Linux:\n Mac:\r - 
字节流写数据如何实现追加写入呢?
public FileOutputStream(String name,boolean append)
 创建文件输出流以指定的名称写入文件。
 如果第二个指定的参数为true,则将写入文件的末尾而不是开头
 
5.字节流数据加异常处理
- try/catch处理

 
//catch块  
} catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos != null){
                try {
                   fos.close();//关闭资源
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
6.字节流读数据(一次读一个字节数据)
- 
需求:把文件中的fos.txt中的内容读取出来在控制台输出
FileInputStream:从文件系统中输入字节
 - 
FileInputStream(String name):通过打开与实际文件连接来创建一个FileInputStream,该文件由文件系统中的路径名name命名
 - 
使用步骤:
- 1.创建字节输入流对象
 - 2.调用字节输入流对像的读数据方法
 - 3.释放资源
 
 
//字节流读数据标准写法
int by;
while((by=fis.read()) != -1){
    System.out.print((char)by);
}
案例:复制文本文件
分析:

- 思路:

 
练习:
 public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("E:\\2021Study\\test.txt");
        FileOutputStream fos = new FileOutputStream("基础语法\\test.txt");
        int by;
        while ((by=fis.read())!= -1){
            fos.write(by);
        }
        fos.close();
        fis.close();
    }
运行结果:

案例:字节流读数据
package com.guoba.day1222;
import java.io.FileInputStream;
import java.io.IOException;
/*
    需求:
        把文件fos.txt中的内容读取出来在控制台输出
    使用字节流输入数据的步骤:
        1.创建字节输入流对象
        2.调用字节输入流对象的读数据方法
        3.释放资源
 */
public class Demo01 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("基础语法\\fis.txt");
//        byte[] bytes = new byte[5];
//
//        int readlen = fis.read(bytes);
//        System.out.println(readlen);
//        System.out.println(new String(bytes));
//
//        readlen = fis.read(bytes);
//        System.out.println(readlen);
//        System.out.println(new String(bytes));
//
//        readlen = fis.read(bytes);
//        System.out.println(readlen);
//        System.out.println(new String(bytes));
//
//        readlen = fis.read(bytes);
//        System.out.println(readlen);
//        System.out.println(new String(bytes));
        byte[] bytes = new byte[1024];//数组长度为1024或其整数倍
        int len;
        //循环改进
        while((len = fis.read(bytes))!=-1){
            System.out.println(new String(bytes,0,len));
        }
        fis.close();
    }
}
案例:复制图片
package com.guoba.day1222;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
    复制图片
    需求:把  数据源图片  复制到 目标地址
    思路:
        1.根据数据源创建字节输入流对象
        2.根据目的地创建字节输入流对象
        3.读写数据,复制图片(一次读取一个字节数组,一次写入一个字节数组)
 */
public class Demo02 {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("E:\\2021Study\\西施.jpg");
        FileOutputStream fos = new FileOutputStream("基础语法\\西施.jpg");
        byte[] bytes = new byte[2048];
        int len;
        while ((len=fis.read(bytes))!=-1){
            fos.write(bytes);
        }
    }
}
7.字节缓冲流
字节缓冲流:
- BufferOutputStream:该类实现缓冲输出流。通过设置这样的输出流,应用程序可以向底层输出流写入字节,而不必为写入的每个字节导致底层系统的调用
 - BufferedInputStream:创建BufferdedInputStream将创建一个内部缓冲区数组。当从流中读取或跳过字节时,内部缓冲区将根据需要从所包含的输入流中重新填充,一次很多字节
 
构造方法:
- 字节缓冲输出流:BufferedOutputStream(OutputStream out)
 - 字节缓冲输入流:BufferedInputStream(InputStream in)
 
package com.guoba.day1222;
import java.io.*;
public class Demo03 {
    public static void main(String[] args) throws IOException {
        //创建字节缓冲输出流对象
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("基础语法\\bos.txt"));
        //写数据
        bos.write("hello\r\n".getBytes());
        bos.write("world\r\n".getBytes());
        //释放资源
        bos.close();
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("基础语法\\bos.txt"));
        //一次读一个字节
        int by;
        while ((by=bis.read())!= -1){
            System.out.print((char)by);
        }
        //一次读一个字节数组
        byte[] bytes = new byte[1024];
        int len;
        while ((len = bis.read(bytes)) != -1) {
            System.out.print(new String(bytes,0,len));
        }
        bis.close();
    }
}
案例:复制视频
package com.guoba.day1222;
import java.io.*;
/*
    需求:把 数据源视频 复制到 目标地址
    思路:
        1.根据数据源创建字节输入流对象
        2.根据目的地创建字节输出流对象
        3.对写数据,复制视频
        4.释放资源
        方式:
        》基本字节流一次读取一个字节
        》基本字节流一次读写一个字节数组
        》字节缓冲流一次读写一个字节
        》字节缓冲流一次读写一个字节数组
 */
public class Demo04_CopyVideo {
    public static void main(String[] args) throws IOException{
        long startTime = System.currentTimeMillis();//记录开始时间
        //method1();
        //method2();
        //method3();
        method4();
        long endTime = System.currentTimeMillis();//记录结束时间
        System.out.println("共耗时:"+(endTime-startTime)+"毫秒");
    }
    //字节流一次读取一个字节:共耗时:25807毫秒
    public static void method1()throws IOException {
        FileInputStream fis = new FileInputStream("E:\\2021Study\\test.mp4");
        FileOutputStream fos = new FileOutputStream("基础语法\\test.mp4");
        int by;
        while ((by=fis.read())!= -1){
            fos.write(by);
        }
        fis.close();
        fos.close();
    }
    //字节流一次读写一个字节数组:共耗时:61毫秒
    public static void method2()throws IOException {
        FileInputStream fis = new FileInputStream("E:\\2021Study\\test.mp4");
        FileOutputStream fos = new FileOutputStream("基础语法\\test.mp4");
        byte[] bytes = new byte[1024];
        int len;
        while ((len=fis.read(bytes))!= -1){
            fos.write(bytes,0,len);
        }
        fis.close();
        fos.close();
    }
    //字节缓冲流一次一字节:共耗时:210毫秒
    public static void method3()throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\2021Study\\test.mp4"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("基础语法\\test.mp4"));
        int by;
        while ((by=bis.read())!=-1){
            bos.write(by);
        }
        bos.close();
        bis.close();
    }
    //字节缓冲流一次一字节数组:共耗时:714毫秒
    public static void method4()throws IOException {
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream("E:\\2021Study\\test.mp4"));
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("基础语法\\test.mp4"));
        byte[] bytes = new byte[1024];
        int len;
        while ((len=bis.read())!= -1){
           bos.write(bytes,0,len);
        }
        bos.close();
        bis.close();
    }
}

                
            
浙公网安备 33010602011771号