Java中常用的IO流总结

  1. FileInputStream/FileOutputStream:这些类用于从/向文件读取/写入二进制数据。它们通常用于读取/写入图像、音频、视频等。
FileInputStream fis = new FileInputStream("path/to/file");
FileOutputStream fos = new FileOutputStream("path/to/file");
  1. FileReader/FileWriter:这些类用于从/向文件读取/写入字符数据。它们通常用于读取/写入文本文件。
FileReader fr = new FileReader("path/to/file");
FileWriter fw = new FileWriter("path/to/file");
  1. BufferedInputStream/BufferedOutputStream:这些类用于从/向文件读取/写入二进制数据,但添加了缓冲以提高性能。
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("path/to/file"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("path/to/file"));
  1. BufferedReader/BufferedWriter:这些类用于从/向文件读取/写入字符数据,但添加了缓冲以提高性能。
BufferedReader br = new BufferedReader(new FileReader("path/to/file"));
BufferedWriter bw = new BufferedWriter(new FileWriter("path/to/file"));
  1. DataInputStream/DataOutputStream:这些类用于从/向文件读取/写入基本数据类型。
DataInputStream dis = new DataInputStream(new FileInputStream("path/to/file"));
DataOutputStream dos = new DataOutputStream(new FileOutputStream("path/to/file"));
  1. ObjectInputStream/ObjectOutputStream:这些类用于从/向文件读取/写入Java对象。
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("path/to/file"));
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("path/to/file"));
  1. PrintStream/PrintWriter:这些类用于将格式化的表示形式写入输出流。它们通常用于将文本输出到控制台或文件。
PrintStream ps = new PrintStream(new FileOutputStream("path/to/file"));
PrintWriter pw = new PrintWriter(new FileWriter("path/to/file"));
  1. PushbackInputStream/PushbackReader:这些类用于在读取数据时能够将数据推回到输入流中。它们通常用于解析数据。
PushbackInputStream pbis = new PushbackInputStream(new FileInputStream("path/to/file"));
PushbackReader pbr = new PushbackReader(new FileReader("path/to/file"));
  1. SequenceInputStream:这个类用于将多个输入流串联在一起,以便将它们视为单个输入流。它通常用于将多个文件合并为一个文件。
SequenceInputStream sis = new SequenceInputStream(new FileInputStream("path/to/file1"), new FileInputStream("path/to/file2"));
  1. File:这个类用于表示文件和目录的路径名。它通常用于检查文件是否存在、创建新文件或目录等操作。
File file = new File("path/to/file");

这些是Java中一些其他常用的IO流类。