2021-04-27 IO流概述

IO流概述
  • IO:输入/输出(Iuput/Output)
  • 流:是一种抽象概念,是对数据传输的总称。也就是说数据在设备间的传输称为流,流的本质是数据传输
  • IO流就是用来处理设备间数据传输问题的
    其中常见的应用:文本复制;文件上传;文本下载
IO流分类
  • 按照数据的流向
    输入流:读数据
    输出流: 写数据
  • 按照数据类型来分
    字节流: 字节输入流,字节输出流
    字符流: 字符输入流,字符输出流

一般来说,我们说IO流的分类是按照数据类型来分的

  • 如果数据通过Windows自带的记事本软件打开,我们还可以读懂里面的内容,就使用字符流,否则使用字节流。如果你不知道该使用哪种类型的流,就使用字节流。
字节流写数据

字节流抽象基类

  • InputStream: 这个抽象类是表示字节输入流的所有类的超类
  • OutputStream: 这个抽象类是表示字节输出流的所有类的超类
  • 子类名特点:子类名称都是以其父类名作为子类名的后缀

FileOutputStream:文件输出流用于数据写入File

  • FileOutputStream(String name) : 创建文件输出流以指定的名称写入文件
使用字节输出流写数据的步骤:
  • 创建字节输出流对象(调用系统功能创建了文件,创建字节输出流对象,让字节输出流对象指向文件)
  • 调用字节输出流对象的写数据方法
  • 释放资源(关闭此文件输出流并释放与此流相关联的任何系统资源)
字节流写数据的3种方式
方法名 说明
void write (int b) 将指定的字节写入此文件输出流,一次写一个字节数据。
void write (byte[] b) 将 b.length字节从指定的字节数组写入此文件输出流,一次写一个字节数组数据。
void write (byte[] b, int off, int len) 将 len字节从指定的字节数组开始,从偏移量 off开始写入此文件输出流,一次写一个字节数组的部分数据。
  // void write (int b) 将指定的字节写入此文件输出流。
      fos.write(95);
      fos.write(65);
      // //void write (byte[] b) 将 b.length字节从指定的字节数组写入此文件输出流
      byte[] b = {97,98,99,100,101};
      // byte[] getByte():返回字符串对应的字节数组
      byte[] bys = "abcde".getBytes();
//        fos.write(bys);
      //void write (byte[] b, int off, int len) 将 len字节从指定的字节数组开始,从偏移量 off开始写入此文件输出流。
      fos.write(bys,1,3);
字节流写数据的两个小问题

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

  • 写完数据后,加换行符
    Windows:\n
    linus: \n
    mac: \r

字节流写数据如何实现追加呢?

  • public FileOutputStream(String name,boolean append)
  • 创建文件输出流以指定的名称写入文件,如果第二个参数为true.,则字节将写入文件的末尾而不是开头
import java.io.FileOutputStream;
import java.io.IOException;

public class FileOutputStreamDemo01 {
    public static void main(String[] args) throws IOException {
        FileOutputStream fos = new FileOutputStream("MySet\\FOS1.txt",true);
        // 写数据
        for (int i = 0; i < 10; i++) {
            fos.write("hello".getBytes());
            fos.write("\n".getBytes()); // 实现换行
        }
        //释放资源
        fos.close();
    }
}

运行两次产生20遍hello

字节流写数据加异常处理

finally: 在异常处理时提供finally块来执行所有清除操作。比如说IO流中的释放资源
特点:被finally控制的语句一定会执行,除非JVM退出

try{
    可能出现异常的代码;
}catch (异常类名 变量名) {
异常的处理代码;
}finally {
    执行所有的清除操作;
}  
public class FileOutputStreamDemo02 {
    public static void main(String[] args) {
        FileOutputStream fos11 = null;
        try {
            fos11 = new FileOutputStream("MySet\\fos11.txt");
            fos11.write("hello".getBytes());
            fos11.close();
        }catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fos11 != null) { // 为了程序的健壮性;对fos11进行不为空的判断
                try {
                    fos11.close();
                } catch (IOException e) {  // 抛出close的异常
                    e.printStackTrace();
                }
            }
        }
    }
}
字节流读数据(一次读一个字节数据)

需求:把文件fos.txt中的内容读取出来在控制台输出
FileInputStream: 从文件系统中的文件获取输入字节

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

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

  1. 创建字节输入流对象
  2. 调用字节输入流对象的读数据方法
  3. 释放资源
public class FileOutputStreamDemo01 {
    public static void main(String[] args) throws IOException {
        FileInputStream fs = new FileInputStream("MySet\\fos.txt");
        // int read():从该输入流读取一个字节的数据
        // 第一次读取数据
//        int by = fs.read();
//        System.out.println(by);
//        System.out.println((char) by); // 强制转化
//
//        // 第二次读取数据
//        by = fs.read();
//        System.out.println(by);
//        System.out.println((char) by);
//
//        by = fs.read();
//        System.out.println(by); // 如果到达文件的末尾,-1
        /*
        * 循环改进条件*/
//        int b = fs.read();
//        while (b != -1) {
//            System.out.print((char) b);
//            b = fs.read();
//        }
        int by ;
        /*
        * fs.read() : 读数据
        *  by = fs.read() :把读取到的数据赋值给by
        *  by != -1 :判断读取到的数据是否是-1*/
        while ((by=fs.read()) != -1) {
            System.out.print((char) by);
        }
        // 释放资源
        fs.close();
    }
}
案例:复制文本文件

需求: 把"F:\itcast\java.txt"复制到模块目录(MySet)的“java.txt”

public class FileOutputStreamDemo01 {
    public static void main(String[] args) throws IOException {
        FileInputStream fin = new FileInputStream("F:\\itcast\\java.txt");
        FileOutputStream fos = new FileOutputStream("MySet\\java.txt");
        int by ;
        while ((by= fin.read()) != -1) {
            fos.write(by);
        }
        // 释放资源
        fos.close();
        fin.close();
    }
}
字节流读数据(一次读一个字节数组数据)

需求: 把文件fos.txt中的内容读取出来在控制台输出
使用字节输入流读数据的步骤:

  1. 创建字节输入流对象
  2. 调用字节输入流对象的读数据方法
  3. 释放资源
    public static void main(String[] args) throws IOException {
        //创建字节输入流对象
        FileInputStream fis = new FileInputStream("MySet\\fos.txt");
        // 调用字节输入流对象的读数据方法
        //  int read(byte[] b) : 从该输入流读取最多b.length个字节的数据为字节数组。
        byte[] bys = new byte[5];
        // `第一次读取数据
        int len = fis.read(bys);
        System.out.println(len);
        System.out.println(new String(bys,0,len));
         //第二次读取数据
        len = fis.read(bys);
        System.out.println(len);
        System.out.println(new String(bys,0,len));// 把字节数组转化成字符串
        len = fis.read(bys);
        System.out.println(len);
        System.out.println(new String(bys,0,len));

//        byte[] bys = new byte[30];// 1024及其整数倍
//        int len;
//        while ((len = fis.read(bys))!= -1){
//            System.out.print(new String(bys,0,len));
//        }
        // 释放资源
        fis.close();
    }
}
代码运行截图.png
复制图片
public class CopyDemo {
    public static void main(String[] args) throws IOException {
        FileInputStream fis = new FileInputStream("F:\\itcast\\heima.jpg");
        FileOutputStream fos = new FileOutputStream("MySet\\heima.jpg");
        byte[] bys = new byte[2048] ;
        int len;
        while((len=fis.read(bys))!=-1) {
            fos.write(bys,0,len);
        }
        fis.close();
        fos.close();

    }
}
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容