首页 > 其他 > 详细

IO流

时间:2019-07-14 21:37:34      阅读:77      评论:0      收藏:0      [点我收藏+]

字节流

  字节流以字节为单位进行数据处理,读写二进制数据时会使用字节流。字节流的顶端是两个抽象类: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:关闭字节输入流。

  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:关闭字节输出流。

  常用的字节流有:

文件流

  文件流包括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:传入文件路径创建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对象。

技术分享图片
 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

  输出结果:

  技术分享图片

字符流

IO流

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!