IO流

字节流

  字节流以字节为单位进行数据处理,读写二进制数据时会使用字节流。字节流的顶端是两个抽象类:java.io.InputStream和java.io.OutputStream。

  InputStream类常用的方法有:

  1.int read() throws IOException:从当前位置开始读取一个字节并返回。读取不到字节则返回-1。

  2.int read(byte b[]) throws IOException:从当前位置开始读取最多字节数组长度个字节,保存到字节数组,并返回读取到的字节数。读取不到字节则返回-1。

  3.int read(byte b[], int off, int len) throws IOException:从当前位置开始读取最多len个字节,从b[off]开始保存到字节数组,并返回读取到的字节数。读取不到字节则返回-1。

  4.void close() throws IOException:关闭InputStream。

  OutputStream类常用的方法有:

  1.void write(int b) throws IOException:写入一个字节。

  2.void write(byte b[]) throws IOException:写入一个字节数组中的所有字节。

  3.void write(byte b[], int off, int len) throws IOException:写入一个字节数组中从b[off]开始之后的len个字节。

  4.void close() throws IOException:关闭OutputStream。

  常用的字节流有:

文件字节流

  文件字节流包括java.io.FileInputStream类和java.io.FileOutputStream类。文件字节流通过传入文件路径或其File对象创建对象。

  FileInputStream类常用的构造方法有:

  1.FileInputStream(String name) throws FileNotFoundException:传入文件路径创建FileInputStream对象。

  2.FileInputStream(File file) throws FileNotFoundException:传入File对象创建FileInputStream对象。

  创建FileInputStream对象时,如果文件不存在,则会抛出FileNotFoundException异常。

  FileOutputStream类常用的构造方法有:

  1.FileOutputStream(String name) throws FileNotFoundException:传入文件路径创建FileOutputStream对象。

  2.FileOutputStream(File file) throws FileNotFoundException:传入File对象创建FileOutputStream对象。

  创建FileOutputStream对象时,如果文件不存在,则会创建对应的文件。

 1 @Test
 2 void testFileIOStream() {
 3     try {
 4         File file = new File("src\test.txt");
 5         System.out.println("是否存在?" + file.exists());
 6         FileOutputStream fos = new FileOutputStream(file);
 7         System.out.println("是否存在?" + file.exists());
 8         fos.write("Hello World".getBytes());
 9         fos.close();
10         FileInputStream fis = new FileInputStream(file);
11         byte[] b = new byte[11];
12         System.out.println("读取字节数:" + fis.read(b));
13         System.out.println(new String(b));
14         fis.close();
15     } catch (IOException e) {
16         e.printStackTrace();
17     }
18 }
testFileIOStream

  输出结果:

  

数据流

  数据流包括java.io.DataInputStream类和java.io.DataOutputStream类,支持基本数据类型和编码格式为UTF-8的String类型数据的读写。数据流通过包装对应的字节流创建对象。

  DataInputStream类常用的构造方法有:

  1.DataInputStream(InputStream in):传入一个InputStream对象创建DataInputStream对象。

  DataInputStream类常用的方法有:

  1.boolean readBoolean() throws IOException:读取一个boolean型数据。

  2.byte readByte() throws IOException:读取一个byte型数据。

  3.short readShort() throws IOException:读取一个short型数据。

  4.char readChar() throws IOException:读取一个char型数据。

  5.int readInt() throws IOException:读取一个int型数据。

  6.long readLong() throws IOException:读取一个long型数据。

  7.float readFloat() throws IOException:读取一个float型数据。

  8.double readDouble() throws IOException:读取一个double型数据。

  9.String readUTF() throws IOException:读取一个String型数据,该字符串编码格式为UTF-8。

  DataOutputStream类常用的构造方法有:

  1.DataOutputStream(OutputStream out)

  DataOutputStream类常用的方法有:

  1.void writeBoolean(boolean v) throws IOException:写入一个boolean型数据。

  2.void writeByte(int v) throws IOException:写入一个byte型数据。传入int型数据,但是会自动截取后8位数据。

  3.void writeShort(int v) throws IOException:写入一个short型数据。传入int型数据,但是会自动截取后16位数据。

  4.void writeChar(int v) throws IOException:写入一个char型数据。

  5.void writeInt(int v) throws IOException:写入一个int型数据。

  6.void writeLong(long v) throws IOException:写入一个long型数据。

  7.void writeFloat(float v) throws IOException:写入一个float型数据。

  8.void writeDouble(double v) throws IOException:写入一个double型数据。

  9.void writeUTF(String str) throws IOException:写入一个String型数据,该字符串的编码格式为UTF-8。

 1 @Test
 2 void testDataIOStream() {
 3     try {
 4         DataOutputStream dos = new DataOutputStream(new FileOutputStream("src\test.txt"));
 5         dos.writeUTF("Hello World");
 6         dos.writeChar('a');
 7         dos.close();
 8         DataInputStream dis = new DataInputStream(new FileInputStream("src\test.txt"));
 9         System.out.println(dis.readUTF());
10         System.out.println(dis.readChar());
11         dis.close();
12     } catch (IOException e) {
13         e.printStackTrace();
14     }
15 }
testDataIOStream

  输出结果:

  

字节缓冲流

  字节缓冲流包括java.io.BufferedInputStream类和java.io.BufferedOutputStream类。字节缓冲流通过包装对应的字节流创建对象,在创建时会定义一个缓冲区,BufferedInputStream会将数据读入缓冲区,每次读取数据时将从缓冲区中读取;BufferedOutputStream会将数据保存在缓冲区,直到调用flush()方法刷新缓冲区。

  BufferedInputStream类常用的构造方法有:

  1.BufferedInputStream(InputStream in):传入InputStream对象,采用默认缓冲区大小(8192字节),创建BufferedInputStream对象。

  2.BufferedInputStream(InputStream in, int size):传入InputStream对象,并定义缓冲区大小,创建BufferedInputStream对象。

  BufferedOutputStream类常用的构造方法有:

  1.BufferedOutputStream(OutputStream out):传入OutputStream对象,采用默认缓冲区大小(8192字节),创建BufferedOutputStream对象。

  2.BufferedOutputStream(OutputStream out, int size):传入OutputStream对象,并定义缓冲区大小,创建BufferedOutputStream对象。

  BufferedOutputStream类常用的方法有:

  1.void flush() throws IOException:刷新缓冲区。

 1 @Test
 2 void testBufferedIOStream() {
 3     try {
 4         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("src\test.txt"));
 5         bos.write("Hello World".getBytes());
 6         bos.flush();   // 刷新缓冲区
 7         bos.close();
 8         BufferedInputStream bis = new BufferedInputStream(new FileInputStream("src\test.txt"));
 9         byte[] b = new byte[11];
10         System.out.println("读取字节数:" + bis.read(b));
11         System.out.println(new String(b));
12         bis.close();
13     } catch (IOException e) {
14         e.printStackTrace();
15     }
16 }
testBufferedIOStream

  输出结果:

  

对象流

  对象流包括java.io.ObjectInputStream类和java.io.ObjectOutputStream类,用于读写对象。读写对象实际上是字节流和对象之间的转换,即序列化和反序列化的过程。序列化是将一个对象按某种方式转为字节流,反序列化则是将序列化形式的字节流还原为一个对象。一个类需要实现java.io.Serializable接口,其对象才可以被序列化。对象流通过包装对应的字节流创建对象。

  ObjectInputStream类常用的构造方法有:

  1.ObjectInputStream(InputStream in) throws IOException:包装一个InputStream对象创建ObjectInputStream对象。

  ObjectInputStream类常用的方法有:

  1.Object readObject() throws IOException, ClassNotFoundException:读取一个对象。

  ObjectOutputStream类常用的构造方法有:

  1.ObjectOutputStream(OutputStream out) throws IOException:包装一个OutputStream对象创建ObjectOutputStream对象。

  ObjectOutputStream类常用的方法有:

  1.void writeObject(Object obj) throws IOException:写入一个对象。

 1 class Student implements Serializable {
 2 
 3     private static final long serialVersionUID = 1L;
 4     private String name;
 5     private int age;
 6 
 7     public Student(String name, int age) {
 8         this.name = name;
 9         this.age = age;
10     }
11 
12     @Override
13     public String toString() {
14         return "Student [name=" + name + ", age=" + age + "]";
15     }
16 
17 }
Student
 1 @Test
 2 void testObjectIOStream() {
 3     try {
 4         ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("src\test.txt"));
 5         oos.writeObject(new Student("Tom", 12));
 6         oos.close();
 7         ObjectInputStream ois = new ObjectInputStream(new FileInputStream("src\test.txt"));
 8         Student student = (Student) ois.readObject();
 9         System.out.println(student);
10         ois.close();
11     } catch (ClassNotFoundException | IOException e) {
12         e.printStackTrace();
13     }
14 }
testObjectIOStream

  输出结果:

  

字符流

  字符流以Unicode码表示的字符为单位进行数据处理,通常进行文本数据读写时会使用字符流。字符流的顶端是两个抽象类:java.io.Reader和java.io.Writer。

  Reader类常用的方法有:

  1.int read() throws IOException:读取一个字符并返回。读取不到字符则返回-1。

  2.int read(char cbuf[]) throws IOException:从当前位置读取最多字符数组长度个字符,保存到字符数组,并返回读取到的字符数。读取不到字符则返回-1。

  3.int read(char cbuf[], int off, int len) throws IOException:从当前位置读取最多len个字符,从cbuf[off]开始保存到字符数组,并返回获取到的字符数。读取不到字符则返回-1。

  4.void close() throws IOException:关闭Reader。

  Writer类常用的方法有:

  1.void write(int c) throws IOException:写入一个字符。

  2.void write(char cbuf[]) throws IOException:写入字符数组中的所有字符。

  3.void write(char cbuf[], int off, int len) throws IOException:写入从cbuf[off]开始的len个字符。

  4.void write(String str) throws IOException:写入一个字符串。

  5.void write(String str, int off, int len) throws IOException:写入从位标为off的字符开始的len个字符。

  6.void close() throws IOException:关闭Writer。

  常用的字符流有:

转换流

  转换流包括java.io.InputStreamReader类和java.io.OutputStreamWriter类,将一个字节流转换为对应的字符流。转换流通过包装对应的字节流创建对象。

  InputStreamReader类常用的构造方法有:

  1.InputStreamReader(InputStream in):包装InputStream对象创建InputStreamReader对象。

  2.InputStreamReader(InputStream in, String charsetName) throws UnsupportedEncodingException:包装InputStream对象并传入字符集名创建InputStreamReader对象。

  InputStreamReader类常用的方法有:

  1.String getEncoding():获取当前的字符集名。

  OutputStreamWriter类常用的构造方法有:

  1.OutputStreamWriter(OutputStream out):包装OutputStream对象创建OutputStreamWriter对象。

  2.OutputStreamWriter(OutputStream out, String charsetName) throws UnsupportedEncodingException:包装OutputStream对象并传入字符集名创建OutputStreamWriter对象。

  OutputStreamWriter类常用的方法有:

  1.String getEncoding():获取当前的字符集名。

 1 @Test
 2 void testISRAndOSW() {
 3     try {
 4         OutputStreamWriter osw = new OutputStreamWriter(new FileOutputStream("src\test.txt"));
 5         osw.write("Hello World");
 6         System.out.println("OutputStreamWriter的字符集:" + osw.getEncoding());
 7         osw.close();
 8         InputStreamReader isr = new InputStreamReader(new FileInputStream("src\test.txt"));
 9         char[] cbuf = new char[11];
10         System.out.println("InputStreamReader的字符集:" + isr.getEncoding());
11         System.out.println("读取的字符数:" + isr.read(cbuf));
12         System.out.println(new String(cbuf));
13         isr.close();
14     } catch (IOException e) {
15         e.printStackTrace();
16     }
17 }
testISRAndOSW

  输出结果:

  

文件字符流

  文件字节流包括java.io.FileReader类和java.io.FileWriter类,这两个类分别继承了InputStreamReader类和OutputStreamWriter类。文件字符流通过传入文件路径或其File对象创建对象。

  FileReader类常用的构造方法有:

  1.FileReader(String fileName) throws FileNotFoundException:传入文件路径创建FileReader对象。

  2.FileReader(String fileName, Charset charset) throws IOException:传入文件路径和字符集名创建FileReader对象。

  3.FileReader(File file) throws FileNotFoundException:传入File对象创建FileReader对象。

  4.FileReader(File file, Charset charset) throws IOException:传入File对象和字符集名创建FileReader对象。

  创建FileReader对象时,如果文件不存在,则会抛出FileNotFoundException异常。

  FileWriter类常用的构造方法有:

  1.FileWriter(String fileName) throws IOException:传入文件路径创建FileWriter对象。

  2.FileWriter(String fileName, Charset charset) throws IOException:传入文件路径和字符集名创建FileWriter对象。

  3.FileWriter(File file) throws IOException:传入File对象创建FileWriter对象。

  4.FileWriter(File file, Charset charset) throws IOException:传入File对象和字符集名创建FileWriter对象。

  创建FileWriter对象时,如果文件不存在,则会创建对应的文件。

 1 @Test
 2 void testFileRW() {
 3     try {
 4         File file = new File("src\test.txt");
 5         System.out.println("是否存在?" + file.exists());
 6         FileWriter fw = new FileWriter(file);
 7         System.out.println("是否存在?" + file.exists());
 8         fw.write("Hello World");
 9         fw.close();
10         FileReader fr = new FileReader(file);
11         char[] cbuf = new char[11];
12         System.out.println("读取的字符数:" + fr.read(cbuf));
13         System.out.println(new String(cbuf));
14         fr.close();
15     } catch (IOException e) {
16         e.printStackTrace();
17     }
18 }
testFileRW

  输出结果:

  

字符缓冲流

   字符缓冲流包括java.io.BufferedReader类和java.io.BufferedWriter类。字符缓冲流通过包装对应的字符流创建对象,在创建时会定义一个缓冲区,BufferedReader会将数据读入缓冲区,每次读取数据时将从缓冲区中读取;BufferedWriter会将数据保存在缓冲区,直到调用flush()方法刷新缓冲区。

  BufferedReader类常用的构造方法有:

  1.BufferedReader(Reader in):传入Reader对象,采用默认缓冲区大小(8192字节),创建BufferedReader对象。

  2.BufferedReader(Reader in, int sz):传入Reader对象,并定义缓冲区大小,创建BufferedReader对象。

  BufferedReader类常用的方法有:

  1.String readLine() throws IOException:读取一行。

  BufferedWriter类常用的构造方法有:

  1.BufferedWriter(Writer out):传入Writer对象,采用默认缓冲区大小(8192字节),创建BufferedWriter对象。

  2.BufferedWriter(Writer out, int sz):传入Writer对象,并定义缓冲区大小,创建BufferedWriter对象。

  BufferedWriter类常用的方法有:

  1.void newLine() throws IOException:换行。

  2.void flush() throws IOException:刷新缓冲区。

 1 @Test
 2 void testBufferedRW() {
 3     try {
 4         BufferedWriter bw = new BufferedWriter(new FileWriter("src\test.txt"));
 5         bw.write("Hello");
 6         bw.newLine();
 7         bw.write("World");
 8         bw.flush();   // 刷新缓冲区
 9         bw.close();
10         BufferedReader br = new BufferedReader(new FileReader("src\test.txt"));
11         System.out.println(br.readLine());
12         System.out.println(br.readLine());
13         br.close();
14     } catch (IOException e) {
15         e.printStackTrace();
16     }
17 }
testBufferedRW

  输出结果:

  

原文地址:https://www.cnblogs.com/lqkStudy/p/11185663.html