JAVA(IO流)

 一、Java IO 流概述
Java IO(Input/Output)流是用于处理输入和输出的机制,主要用于读取和写入数据。Java 提供了丰富的 IO 流类,这些类主要分为两大类:字节流和字符流。


1.字节流

输入流:`InputStream`及其子类,用于从源读取字节数据。

输出流:`OutputStream`及其子类,用于向目标写入字节数据。

特点:操作二进制数据,适用于处理图片、音频等二进制文件。


2.字符流

输入流:`Reader`及其子类,用于从源读取字符数据。

输出流:`Writer`及其子类,用于向目标写入字符数据。

特点:操作文本数据,自动处理字符编码,适用于处理文本文件。

 

二、字节流的常用类

1.输入流

`FileInputStream`:用于从文件中读取字节数据。

  FileInputStream fis = new FileInputStream("example.txt");
  int data;
  while ((data = fis.read()) != -1) {
      System.out.print((char) data);
  }
  fis.close();


`BufferedInputStream`:带有缓冲功能的输入流,提高读取效率。

  BufferedInputStream bis = new BufferedInputStream(new FileInputStream("example.txt"));

2.输出流

`FileOutputStream`:用于向文件写入字节数据。

  FileOutputStream fos = new FileOutputStream("example.txt");
  String str = "Hello, World!";
  fos.write(str.getBytes());
  fos.close();


`BufferedOutputStream`:带有缓冲功能的输出流,提高写入效率。

BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("example.txt"));

三、字符流的常用类

1.输入流

`FileReader`:用于从文件中读取字符数据。

 FileReader fr = new FileReader("example.txt");
  int data;
  while ((data = fr.read()) != -1) {
      System.out.print((char) data);
  }
  fr.close();


`BufferedReader`:带有缓冲功能的字符输入流,支持按行读取。

 BufferedReader br = new BufferedReader(new FileReader("example.txt"));
  String line;
  while ((line = br.readLine()) != null) {
      System.out.println(line);
  }
  br.close();

2.输出流

`FileWriter`:用于向文件写入字符数据。

  FileWriter fw = new FileWriter("example.txt");
  fw.write("Hello, World!");
  fw.close();


`BufferedWriter`:带有缓冲功能的字符输出流,支持按行写入。

  BufferedWriter bw = new BufferedWriter(new FileWriter("example.txt"));
  bw.write("Hello, World!");
  bw.newLine(); // 写入换行符
  bw.close();

四、序列化与反序列化

序列化:将对象转换为字节序列的过程,使用`ObjectOutputStream`。

  ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.ser"));
  oos.writeObject(new Person("John", 30));
  oos.close();


反序列化:将字节序列还原为对象的过程,使用`ObjectInputStream`。

  ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.ser"));
  Person person = (Person) ois.readObject();
  ois.close();

五、注意事项

1. 异常处理:IO 操作可能会抛出`IOException`,需要进行异常处理。

  try (FileInputStream fis = new FileInputStream("example.txt")) {
       int data;
       while ((data = fis.read()) != -1) {
           System.out.print((char) data);
       }
   } catch (IOException e) {
       e.printStackTrace();
   }


2. 资源关闭:使用`try-with-resources`语句可以自动关闭资源,避免资源泄漏。

   try (FileInputStream fis = new FileInputStream("example.txt");
        FileOutputStream fos = new FileOutputStream("output.txt")) {
       int data;
       while ((data = fis.read()) != -1) {
           fos.write(data);
       }
   } catch (IOException e) {
       e.printStackTrace();
   }


3. 字符编码:字符流在读写时会涉及字符编码,确保编码一致,避免乱码问题。

 

六、总结
Java IO 流提供了丰富的类和方法,用于处理各种输入输出操作。掌握字节流和字符流的区别,以及常用流的使用方法,可以高效地进行文件操作、网络通信和对象序列化等任务。

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值