IO流:字节流、字符流、缓冲流(超详细)

傷城~ 2024-03-17 12:45 157阅读 0赞

一、字节流

1 一切皆为字节

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

2 字节输出流【OutputStream】

java.io.OutputStream抽象类是表示字节输出流的所有类的超类,将指定的字节信息写出到目的地。它定义了字节输出流的基本共性功能方法。

  • public void close() :关闭此输出流并释放与此流相关联的任何系统资源。
  • public void flush() :刷新此输出流并强制任何缓冲的输出字节被写出。
  • public void write(byte[] b):将 b.length字节从指定的字节数组写入此输出流。
  • public void write(byte[] b, int off, int len) :从指定的字节数组写入 len字节,从偏移量 off开始输出到此输出流。
  • public abstract void write(int b) :将指定的字节输出流。

小贴士:

close方法,当完成流的操作时,必须调用此方法,释放系统资源。

3 FileOutputStream类

java.io.FileOutputStream类是文件输出流,用于将数据写出到文件。

构造方法

  • public FileOutputStream(File file):创建文件输出流以写入由指定的 File对象表示的文件。
  • public FileOutputStream(String name): 创建文件输出流以指定的名称写入文件。

当你创建一个流对象时,必须传入一个文件路径。该路径下,如果没有这个文件,会创建该文件。如果有这个文件,会清空这个文件的数据。

  • 构造举例,代码如下:
  1. public class FileOutputStreamConstructor throws IOException {
  2. public static void main(String[] args) {
  3. // 使用File对象创建流对象
  4. File file = new File("a.txt");
  5. FileOutputStream fos = new FileOutputStream(file);
  6. // 使用文件名称创建流对象
  7. FileOutputStream fos = new FileOutputStream("b.txt");
  8. }
  9. }

写出字节数据

  1. 写出字节write(int b) 方法,每次可以写出一个字节数据,代码使用演示:
  1. public class FOSWrite {
  2. public static void main(String[] args) throws IOException {
  3. // 使用文件名称创建流对象
  4. FileOutputStream fos = new FileOutputStream("fos.txt");
  5. // 写出数据
  6. fos.write(97); // 写出第1个字节
  7. fos.write(98); // 写出第2个字节
  8. fos.write(99); // 写出第3个字节
  9. // 关闭资源
  10. fos.close();
  11. }
  12. }
  13. 输出结果:
  14. abc

小贴士:

  1. 虽然参数为int类型四个字节,但是只会保留一个字节的信息写出。
  2. 流操作完毕后,必须释放系统资源,调用close方法,千万记得。
  1. 写出字节数组write(byte[] b),每次可以写出数组中的数据,代码使用演示:
  1. public class FOSWrite {
  2. public static void main(String[] args) throws IOException {
  3. // 使用文件名称创建流对象
  4. FileOutputStream fos = new FileOutputStream("fos.txt");
  5. // 字符串转换为字节数组
  6. byte[] b = "清华大学".getBytes();
  7. // 写出字节数组数据
  8. fos.write(b);
  9. // 关闭资源
  10. fos.close();
  11. }
  12. }
  13. 输出结果:
  14. 清华大学
  1. 写出指定长度字节数组write(byte[] b, int off, int len) ,每次写出从off索引开始,len个字节,代码使用演示:
  1. public class FOSWrite {
  2. public static void main(String[] args) throws IOException {
  3. // 使用文件名称创建流对象
  4. FileOutputStream fos = new FileOutputStream("fos.txt");
  5. // 字符串转换为字节数组
  6. byte[] b = "abcde".getBytes();
  7. // 写出从索引2开始,2个字节。索引2是c,两个字节,也就是cd。
  8. fos.write(b,2,2);
  9. // 关闭资源
  10. fos.close();
  11. }
  12. }
  13. 输出结果:
  14. cd

数据追加续写

经过以上的演示,每次程序运行,创建输出流对象,都会清空目标文件中的数据。如何保留目标文件中数据,还能继续添加新数据呢?

  • public FileOutputStream(File file, boolean append): 创建文件输出流以写入由指定的 File对象表示的文件。
  • public FileOutputStream(String name, boolean append): 创建文件输出流以指定的名称写入文件。

这两个构造方法,参数中都需要传入一个boolean类型的值,true 表示追加数据,false 表示清空原有数据。这样创建的输出流对象,就可以指定是否追加续写了,代码使用演示:

  1. public class FOSWrite {
  2. public static void main(String[] args) throws IOException {
  3. // 使用文件名称创建流对象
  4. FileOutputStream fos = new FileOutputStream("fos.txt"true);
  5. // 字符串转换为字节数组
  6. byte[] b = "abcde".getBytes();
  7. // 写出从索引2开始,2个字节。索引2是c,两个字节,也就是cd。
  8. fos.write(b);
  9. // 关闭资源
  10. fos.close();
  11. }
  12. }
  13. 文件操作前:cd
  14. 文件操作后:cdabcde

写出换行

Windows系统里,换行符号是\r\n 。把

以指定是否追加续写了,代码使用演示:

  1. public class FOSWrite {
  2. public static void main(String[] args) throws IOException {
  3. // 使用文件名称创建流对象
  4. FileOutputStream fos = new FileOutputStream("fos.txt");
  5. // 定义字节数组
  6. byte[] words = {97,98,99,100,101};
  7. // 遍历数组
  8. for (int i = 0; i < words.length; i++) {
  9. // 写出一个字节
  10. fos.write(words[i]);
  11. // 写出一个换行, 换行符号转成数组写出
  12. fos.write("\r\n".getBytes());
  13. }
  14. // 关闭资源
  15. fos.close();
  16. }
  17. }
  18. 输出结果:
  19. a
  20. b
  21. c
  22. d
  23. e
  • 回车符\r和换行符\n

    • 回车符:回到一行的开头(return)。
    • 换行符:下一行(newline)。
  • 系统中的换行:

    • Windows系统里,每行结尾是 回车+换行 ,即\r\n
    • Unix系统里,每行结尾只有 换行 ,即\n
    • Mac系统里,每行结尾是 回车 ,即\r。从 Mac OS X开始与Linux统一。

4 字节输入流【InputStream】

java.io.InputStream抽象类是表示字节输入流的所有类的超类,可以读取字节信息到内存中。它定义了字节输入流的基本共性功能方法。

  • public void close() :关闭此输入流并释放与此流相关联的任何系统资源。
  • public abstract int read(): 从输入流读取数据的下一个字节。
  • public int read(byte[] b): 从输入流中读取一些字节数,并将它们存储到字节数组 b中 。

小贴士:

close方法,当完成流的操作时,必须调用此方法,释放系统资源。

5 FileInputStream类

java.io.FileInputStream类是文件输入流,从文件中读取字节。

构造方法

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

当你创建一个流对象时,必须传入一个文件路径。该路径下,如果没有该文件,会抛出FileNotFoundException

  • 构造举例,代码如下:
  1. public class FileInputStreamConstructor throws IOException{
  2. public static void main(String[] args) {
  3. // 使用File对象创建流对象
  4. File file = new File("a.txt");
  5. FileInputStream fos = new FileInputStream(file);
  6. // 使用文件名称创建流对象
  7. FileInputStream fos = new FileInputStream("b.txt");
  8. }
  9. }

读取字节数据

  1. 读取字节read方法,每次可以读取一个字节的数据,提升为int类型,读取到文件末尾,返回-1,代码使用演示:
  1. public class FISRead {
  2. public static void main(String[] args) throws IOException{
  3. // 使用文件名称创建流对象
  4. FileInputStream fis = new FileInputStream("read.txt");
  5. // 读取数据,返回一个字节
  6. int read = fis.read();
  7. System.out.println((char) read);
  8. read = fis.read();
  9. System.out.println((char) read);
  10. read = fis.read();
  11. System.out.println((char) read);
  12. read = fis.read();
  13. System.out.println((char) read);
  14. read = fis.read();
  15. System.out.println((char) read);
  16. // 读取到末尾,返回-1
  17. read = fis.read();
  18. System.out.println( read);
  19. // 关闭资源
  20. fis.close();
  21. }
  22. }
  23. 输出结果:
  24. a
  25. b
  26. c
  27. d
  28. e
  29. -1

循环改进读取方式,代码使用演示:

  1. public class FISRead {
  2. public static void main(String[] args) throws IOException{
  3. // 使用文件名称创建流对象
  4. FileInputStream fis = new FileInputStream("read.txt");
  5. // 定义变量,保存数据
  6. int b
  7. // 循环读取
  8. while ((b = fis.read())!=-1) {
  9. System.out.println((char)b);
  10. }
  11. // 关闭资源
  12. fis.close();
  13. }
  14. }
  15. 输出结果:
  16. a
  17. b
  18. c
  19. d
  20. e

小贴士:

  1. 虽然读取了一个字节,但是会自动提升为int类型。
  2. 流操作完毕后,必须释放系统资源,调用close方法,千万记得。
  1. 使用字节数组读取read(byte[] b),每次读取b的长度个字节到数组中,返回读取到的有效字节个数,读取到末尾时,返回-1 ,代码使用演示:
  1. public class FISRead {
  2. public static void main(String[] args) throws IOException{
  3. // 使用文件名称创建流对象.
  4. FileInputStream fis = new FileInputStream("read.txt"); // 文件中为abcde
  5. // 定义变量,作为有效个数
  6. int len
  7. // 定义字节数组,作为装字节数据的容器
  8. byte[] b = new byte[2];
  9. // 循环读取
  10. while (( len= fis.read(b))!=-1) {
  11. // 每次读取后,把数组变成字符串打印
  12. System.out.println(new String(b));
  13. }
  14. // 关闭资源
  15. fis.close();
  16. }
  17. }
  18. 输出结果:
  19. ab
  20. cd
  21. ed

错误数据d,是由于最后一次读取时,只读取一个字节e,数组中,上次读取的数据没有被完全替换,所以要通过len ,获取有效的字节,代码使用演示:

  1. public class FISRead {
  2. public static void main(String[] args) throws IOException{
  3. // 使用文件名称创建流对象.
  4. FileInputStream fis = new FileInputStream("read.txt"); // 文件中为abcde
  5. // 定义变量,作为有效个数
  6. int len
  7. // 定义字节数组,作为装字节数据的容器
  8. byte[] b = new byte[2];
  9. // 循环读取
  10. while (( len= fis.read(b))!=-1) {
  11. // 每次读取后,把数组的有效字节部分,变成字符串打印
  12. System.out.println(new String(b0len));// len 每次读取的有效字节个数
  13. }
  14. // 关闭资源
  15. fis.close();
  16. }
  17. }
  18. 输出结果:
  19. ab
  20. cd
  21. e

小贴士:

使用数组读取,每次读取多个字节,减少了系统间的IO操作次数,从而提高了读写的效率,建议开发中使用。

二、字符流

当使用字节流读取文本文件时,可能会有一个小问题。就是遇到中文字符时,可能不会显示完整的字符,那是因为一个中文字符可能占用多个字节存储。所以Java提供一些字符流类,以字符为单位读写数据,专门用于处理文本文件。

1 字符输入流【Reader】

java.io.Reader抽象类是表示用于读取字符流的所有类的超类,可以读取字符信息到内存中。它定义了字符输入流的基本共性功能方法。

  • public void close() :关闭此流并释放与此流相关联的任何系统资源。
  • public int read(): 从输入流读取一个字符。
  • public int read(char[] cbuf): 从输入流中读取一些字符,并将它们存储到字符数组 cbuf中 。

2 FileReader类

java.io.FileReader类是读取字符文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。

小贴士:

  1. 字符编码:字节与字符的对应规则。Windows系统的中文编码默认是GBK编码表。

    idea中UTF-8

  2. 字节缓冲区:一个字节数组,用来临时存储字节数据。

构造方法

  • FileReader(File file): 创建一个新的 FileReader ,给定要读取的File对象。
  • FileReader(String fileName): 创建一个新的 FileReader ,给定要读取的文件的名称。

当你创建一个流对象时,必须传入一个文件路径。类似于FileInputStream 。

  • 构造举例,代码如下:
  1. public class FileReaderConstructor throws IOException{
  2. public static void main(String[] args) {
  3. // 使用File对象创建流对象
  4. File file = new File("a.txt");
  5. FileReader fr = new FileReader(file);
  6. // 使用文件名称创建流对象
  7. FileReader fr = new FileReader("b.txt");
  8. }
  9. }

读取字符数据

  1. 读取字符read方法,每次可以读取一个字符的数据,提升为int类型,读取到文件末尾,返回-1,循环读取,代码使用演示:
  1. public class FRRead {
  2. public static void main(String[] args) throws IOException {
  3. // 使用文件名称创建流对象
  4. FileReader fr = new FileReader("read.txt");
  5. // 定义变量,保存数据
  6. int b
  7. // 循环读取
  8. while ((b = fr.read())!=-1) {
  9. System.out.println((char)b);
  10. }
  11. // 关闭资源
  12. fr.close();
  13. }
  14. }
  15. 输出结果:

小贴士:虽然读取了一个字符,但是会自动提升为int类型。

  1. 使用字符数组读取read(char[] cbuf),每次读取b的长度个字符到数组中,返回读取到的有效字符个数,读取到末尾时,返回-1 ,代码使用演示:
  1. public class FRRead {
  2. public static void main(String[] args) throws IOException {
  3. // 使用文件名称创建流对象
  4. FileReader fr = new FileReader("read.txt");
  5. // 定义变量,保存有效字符个数
  6. int len
  7. // 定义字符数组,作为装字符数据的容器
  8. char[] cbuf = new char[2];
  9. // 循环读取
  10. while ((len = fr.read(cbuf))!=-1) {
  11. System.out.println(new String(cbuf));
  12. }
  13. // 关闭资源
  14. fr.close();
  15. }
  16. }

获取有效的字符改进,代码使用演示:

  1. public class FISRead {
  2. public static void main(String[] args) throws IOException {
  3. // 使用文件名称创建流对象
  4. FileReader fr = new FileReader("read.txt");
  5. // 定义变量,保存有效字符个数
  6. int len
  7. // 定义字符数组,作为装字符数据的容器
  8. char[] cbuf = new char[2];
  9. // 循环读取
  10. while ((len = fr.read(cbuf))!=-1) {
  11. System.out.println(new String(cbuf,0,len));
  12. }
  13. // 关闭资源
  14. fr.close();
  15. }
  16. }

3 字符输出流【Writer】

java.io.Writer抽象类是表示用于写出字符流的所有类的超类,将指定的字符信息写出到目的地。它定义了字节输出流的基本共性功能方法。

  • void write(int c) 写入单个字符。
  • void write(char[] cbuf)写入字符数组。
  • abstract void write(char[] cbuf, int off, int len)写入字符数组的某一部分,off数组的开始索引,len写的字符个数。
  • void write(String str)写入字符串。
  • void write(String str, int off, int len) 写入字符串的某一部分,off字符串的开始索引,len写的字符个数。
  • void flush()刷新该流的缓冲。
  • void close() 关闭此流,但要先刷新它。

4 FileWriter类

java.io.FileWriter类是写出字符到文件的便利类。构造时使用系统默认的字符编码和默认字节缓冲区。

构造方法

  • FileWriter(File file): 创建一个新的 FileWriter,给定要读取的File对象。
  • FileWriter(String fileName): 创建一个新的 FileWriter,给定要读取的文件的名称。

当你创建一个流对象时,必须传入一个文件路径,类似于FileOutputStream。

  • 构造举例,代码如下:
  1. public class FileWriterConstructor {
  2. public static void main(String[] args) throws IOException {
  3. // 使用File对象创建流对象
  4. File file = new File("a.txt");
  5. FileWriter fw = new FileWriter(file);
  6. // 使用文件名称创建流对象
  7. FileWriter fw = new FileWriter("b.txt");
  8. }
  9. }

基本写出数据

写出字符write(int b) 方法,每次可以写出一个字符数据,代码使用演示:

  1. public class FWWrite {
  2. public static void main(String[] args) throws IOException {
  3. // 使用文件名称创建流对象
  4. FileWriter fw = new FileWriter("fw.txt");
  5. // 写出数据
  6. fw.write(97); // 写出第1个字符
  7. fw.write('b'); // 写出第2个字符
  8. fw.write('C'); // 写出第3个字符
  9. fw.write(30000); // 写出第4个字符,中文编码表中30000对应一个汉字。
  10. /*
  11. 【注意】关闭资源时,与FileOutputStream不同。
  12. 如果不关闭,数据只是保存到缓冲区,并未保存到文件。
  13. */
  14. // fw.close();
  15. }
  16. }
  17. 输出结果:
  18. abC

小贴士:

  1. 虽然参数为int类型四个字节,但是只会保留一个字符的信息写出。
  2. 未调用close方法,数据只是保存到了缓冲区,并未写出到文件中。

关闭和刷新

因为内置缓冲区的原因,如果不关闭输出流,无法写出字符到文件中。但是关闭的流对象,是无法继续写出数据的。如果我们既想写出数据,又想继续使用流,就需要flush 方法了。

  • flush :刷新缓冲区,流对象可以继续使用。
  • close:先刷新缓冲区,然后通知系统释放资源。流对象不可以再被使用了。

代码使用演示:

  1. public class FWWrite {
  2. public static void main(String[] args) throws IOException {
  3. // 使用文件名称创建流对象
  4. FileWriter fw = new FileWriter("fw.txt");
  5. // 写出数据,通过flush
  6. fw.write('刷'); // 写出第1个字符
  7. fw.flush();
  8. fw.write('新'); // 继续写出第2个字符,写出成功
  9. fw.flush();
  10. // 写出数据,通过close
  11. fw.write('关'); // 写出第1个字符
  12. fw.close();
  13. fw.write('闭'); // 继续写出第2个字符,【报错】java.io.IOException: Stream closed
  14. fw.close();
  15. }
  16. }

小贴士:即便是flush方法写出了数据,操作的最后还是要调用close方法,释放系统资源。

三、缓冲流

1、缓冲流,也叫高效流,是对4个基本的 FileXxx 流的增强,所以也是4个流;

分类:

字节缓冲流: BufferedInputStream , BufferedOutputStream

字符缓冲流: BufferedReader , BufferedWriter

基本原理:

是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO次数,从而提高读写的效率

2、字节缓冲流【BufferedInputStream】【BufferedOutputStream】 构造方法:

public BufferedInputStream(InputStream in) : 创建一个 新的缓冲输入流。

public BufferedOutputStream(OutputStream out) : 创建一个新的缓冲输出流。
缓存流的速度比基本的流对象的传输速度快了很多;

3、字符缓冲流【BufferedReader】【BufferedWriter】 构造方法:

public BufferedReader(Reader in) :创建一个 新的缓冲输入流。
public BufferedWriter(Writer out) : 创建一个新的缓冲输出流。

特有的方法:

BufferedReader: public String readLine() : 读一行文字。
BufferedWriter: public void newLine() : 写一行行分隔符,(也就是换行)。

发表评论

表情:
评论列表 (有 0 条评论,157人围观)

还没有评论,来说两句吧...

相关阅读