Java复习笔记: IO流的概述和分类

IO流的概述和分类

1、IO流介绍和分类

  • 以前通过数组或集合,将数据保存在内存中
int a = 10;
int [] arr = new int[] {1,2,3,4,5};
ArrayList<String> list = new ArrayList<>();
1)存在的问题

不能永久化存储,只要代码运行结束,所有数据都会丢失。

2)想永久存储数据怎么办呢?

将数据存储在文件中。

3)IO流的概念
  • I表示intput,输入。
  • O表示output,输出。
4)IO流的作用
  • 将数据写到文件中,实现数据永久化存储
  • 读取文件中的数据到程序
5)按流向分
  • 输入流
  • 输出流
6)按操作数据类型分
  • 字节流
  • 字符流

在这里插入图片描述

7)IO流的体系结构:

在这里插入图片描述

2、字节流

一切文件数据(文本、图片、视频等)在存储时,都是以二进制数字的形式保存,都是一个一个的字节,那么传输时一样
如此。所以,字节流可以传输任意文件数据。在操作流的时候,我们要时刻明确,无论使用什么样的流对象,底层传
输的始终为二进制数据。

1)字节输出流的顶级父类OutputStream是抽象的, 我们可以使用它的子类FileOutputStream操作文件
方法名说明
void write(int b)一次写一个字节数据
void write(byte[] b)一次写一个字节数组数据
void write(byte[] b, int off, int len)一次写一个字节数组的部分数据
2)字节流写数据的步骤

①创建字节输出流对象 ②写数据 ③释放资源

public static void main(String[] args) throws IOException 
    // 1.创建IO流
    FileOutputStream fos = new FileOutputStream("study_day11\\abc\\1.txt");
    // 2.写数据
    fos.write(97);
    fos.write(98);
    // 3.关闭    
    fos.close();
}
3)字节流写数据说明

IO流类似水流,当创建IO流后,相当于程序和文件建立了管道

在这里插入图片描述

4)字节流写多个字节数据

方法名说明
void write(byte[] b)一次写一个字节数组数据
void write(byte[] b, int off, int len)一次写一个字节数组的部分数据
public static void main(String[] args) throws IOException {
    FileOutputStream fos = new FileOutputStream("study_day11\\abc\\2.txt");
    byte[] bs = new byte[] {65, 66, 67, 68, 69};
    // void write(byte[] b) 将byte数组写入流中    
    fos.write(bs);
    // void write(byte[] b, int off, int len) 将byte数组的一部分写入流中
    // int off: 从哪个索引开始
    // int len: 写几个
    fos.write(bs, 2, 3);    
    fos.close();
}
5)字节流读一个字节数据

字节输入流的顶级父类InputStream是抽象的, 我们可以使用它的子类FileInputStream操作文件

方法名说明
int read()从流中读取一个字节
int read(byte[] b)从流中读取多个字节保存参数的数组中
6)字节流读数据的步骤

①创建字节输入流对象 ②读取数据 ③释放资源

public static void main(String[] args) throws IOException {
    // 1.创建IO
    FileInputStream fis = new FileInputStream("study_day11\\abc\\3.txt");
    // 定义一个变量保存读取到的数据
    int b = fis.read();
    System.out.println((char) b);
        // 3.关闭
    fis.close();
}
7)如果要将文件中的所有字节数据读取出来,需要使用循环。
public static void main(String[] args) throws IOException {
    // 1.创建IO流
    FileInputStream fis = new FileInputStream("study_day11\\abc\\3.txt");
    // 定义一个变量保存每次读取到的数据
    int b;
    // fis.read(): 读取文件中的内容
    // b = fis.read(): 将读取的内容保存到变量b中
    // (b = fis.read()) != -1: 判断变量b的值是否为-1
    while ((b = fis.read()) != -1) {
    // 读取到内容
    System.out.println((char) b);
    }
    // 3.关闭
    fis.close();
}
8)String和byte数组转换API
  • String转成byte[]
方法名说明
byte[] getBytes()将String的内容转成byte[]
  • byte[]转成String
方法名说明
String(byte bytes[])将byte[]的内容转成String
String(byte bytes[], int offset, int length)将byte[]的部分内容转成String
9)字节流读多个字节数据

字节流读数据的步骤: ①创建字节输入流对象 ②读取数据 ③释放资源

public static void main(String[] args) throws IOException {
    FileInputStream fis = new FileInputStream("study_day11\\abc\\3.txt");
    byte[] buf = new byte[4]; // buf = {0, 0, 0, 0}
    int len = fis.read(buf); // buf = {h, e, l, l}
    System.out.println(len); // 4
    System.out.println(new String(buf));      
    fis.close();
}
  • 循环读取多个字节
public static void main(String[] args) throws IOException {
    FileInputStream fis = new FileInputStream("study_day11\\abc\\3.txt");
    // 定义数组保存读取的内容 数组的长度实际应该写1024的整数倍,建议写1024 * 8
    byte[] buf = new byte[4];
    // 定义一个变量保存读取的数量
    int len;
    while ((len = fis.read(buf)) != -1) {
    // 读取到内容
    System.out.println(new String(buf, 0, len));
    }
    fis.close();
}

3、字符流

因为字节流一次读一个字节,而不管GBK还是UTF-8一个中文都是多个字节,用字节流每次只能读其中的一部分,所以就会出现乱码问题。

1)字符流读一个字符

字符输入流的顶级父类Reader是抽象的, 我们可以使用它的子类FileReader操作文件。

方法名说明
int read()从流中读取一个字符
int read(char[] b)从流中读取多个字符保存参数的数组中
  • 字符流读字符
方法名说明
int read()从流中读取一个字符
 // int read() 读一个字符
 public static void test01() throws IOException {
     FileReader fr = new FileReader("study_day11\\abc\\4.txt");    
    // 定义变量保存读取的字符
     int ch;
     while ((ch = fr.read()) != -1) {
     System.out.println((char) ch);
    }
     fr.close();
 }
2)字符流读多个字符
方法名说明
int read(char[] b)从流中读取多个字符保存参数的数组中
// int read(char[] cbuf) 读取多个字符
public static void test02() throws IOException {
    FileReader fr = new FileReader("study_day11\\abc\\4.txt");
    // 定义数组保存读取的内容
    char[] chs = new char[3];
    // 定义变量保存读取的数量
    int len;
    while ((len = fr.read(chs)) != -1) {
    System.out.println(new String(chs, 0 , len));
    }
    fr.close();
}	
3)字符流写数据

字符输出流的顶级父类Writer是抽象的, 我们可以使用它的子类File Writer操作文件。

方法名说明
void write(int c)写一个字符到流中
void write(char[] cbuf)写入一个字符数组到流中
void write(char[] cbuf, int off, int len)写入字符数组的一部分到流中
void write(String str)写一个字符串到流中
void write(String str, int off, int len)写一个字符串的一部分到流中
4)数据追加续写和换行
方法名说明
public FileWriter(String name,boolean append)append为true, 以追加的形式写数据到文件, 不会删除文件
  • 换行符: windows: \r\n linux: \n mac: \r
public static void main(String[] args) throws IOException {
    // 第二个参数设置为true,可以追加写数据,不会删除以前的文件
    FileOutputStream fos = new FileOutputStream("study_day11\\abc\\3.txt", true);
    fos.write(97);
    // 换行
    fos.write("\r\n".getBytes());
    fos.write(98);
    fos.close();
}

四、IO流小结:

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值