Java的I/O流可以分为:字节流和字符流。本文重点介绍字节流的基本使用。

流的概念:内存与存储设备之间传输数据的通道。

流的分类——按流向:

输入流:将存储设备中的数据读取到内存中。

输出流:将内存中的数据写入到存储设备中。

 

流的分类——按存储单位:

字节流:以字节为单位,可以读写所有数据。

字符流:以字符为单位,只能读写文本数据。

 

流的分类三——按功能:

节点流:具有实际传输数据的读写功能。

过滤流:在节点流的基础之上增强功能。

 

1.FileInputStream与FileOutputStream

 1 package file.demo;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.IOException;
 5 
 6 //演示FileInputStream的使用
 7 public class filedemo01 {
 8 
 9     public static void main(String[] args) throws IOException {
10 
11         //0.加载文件路径
12         String url = filedemo01.class.getClassLoader().getResource("aaa.txt").getPath();
13         System.out.println("文件位置是:" + url);
14         //1.创建FileInputStream
15         FileInputStream fileInputStream = new FileInputStream(url);
16 
17         int available = fileInputStream.available();
18         System.out.println("可用字节数为:" + available);
19 
20 //        //单个字节读取
21 //        int data = 0;
22 //        while ((data = fileInputStream.read()) != -1) {
23 //            System.out.print((char) data);
24 //        }
25 
26         System.out.println("--------------------------");
27 
28         //一次读取多个字节
29         byte[] bytes = new byte[2];
30         int count = 0;
31         while ((count = fileInputStream.read(bytes)) != -1) {
32             System.out.print(new String(bytes, 0, count));
33         }
34         fileInputStream.close();
35     }
36 }
 1 package file.demo;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.IOException;
 5 
 6 //演示FileOutputStream的使用
 7 public class filedemo02 {
 8     public static void main(String[] args) throws IOException {
 9 
10         //0.
11         String url = filedemo01.class.getClassLoader().getResource("bbb.txt").getPath();
12         System.out.println("文件位置是:" + url);
13         //1.创建FileOutputStream对象
14         FileOutputStream fileOutputStream = new FileOutputStream(url);
15 
16         fileOutputStream.write(97);
17         fileOutputStream.write('b');
18 
19         fileOutputStream.close();
20     }
21 }
 1 package file.demo;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.FileOutputStream;
 5 import java.io.IOException;
 6 
 7 public class filedemo03 {
 8 
 9 
10     public static void main(String[] args) throws IOException {
11         FileInputStream fileInputStream = new FileInputStream("/Users/yangasen/Downloads/aaa.txt");
12 
13         FileOutputStream fileOutputStream = new FileOutputStream("/Users/yangasen/Downloads/ccc.txt");
14 
15         int count = 0;
16         byte[] bytes = new byte[1024];
17         while ((count = fileInputStream.read(bytes)) != -1) {
18             fileOutputStream.write(bytes, 0, count);
19         }
20         fileInputStream.close();
21         fileOutputStream.close();
22 
23     }
24 
25 
26 }

 

2.BufferedInputStream与BufferedOutputStream

缓冲流:提高IO效率,减少访问磁盘次数。数据存储在缓冲区中,flush()将缓冲区内容写入文件。也可以直接close(),会自动调用flush()。

 1 package file.demo;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.FileInputStream;
 5 import java.io.IOException;
 6 
 7 //BufferedInputStream
 8 public class buffereddemo01 {
 9     public static void main(String[] args) throws IOException {
10         FileInputStream fileInputStream = new FileInputStream("/Users/yangasen/Downloads/aaa.txt");
11         BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
12 //        int data = 0;
13 //        while ((data = bufferedInputStream.read()) != -1) {
14 //            System.out.print((char) data);
15 //        }
16         System.out.println("-----------------");
17 
18         byte[] bytes = new byte[1024];
19         int count = 0;
20         while ((count = bufferedInputStream.read(bytes)) != -1) {
21             System.out.println(new String(bytes, 0, count));
22         }
23         bufferedInputStream.close();
24     }
25 }
 1 package file.demo;
 2 
 3 import java.io.BufferedOutputStream;
 4 import java.io.FileOutputStream;
 5 
 6 public class buffereddemo02 {
 7     public static void main(String[] args) throws Exception {
 8         FileOutputStream fileOutputStream = new FileOutputStream("/Users/yangasen/Downloads/ddd.txt");
 9         BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
10 
11         for (int i = 0; i < 10; i++) {
12             bufferedOutputStream.write('z');
13             bufferedOutputStream.flush();
14         }
15         bufferedOutputStream.close();
16     }
17 }

 

3.ObjectOutputStream与ObjectInputStream

对象流增强了缓冲区功能,

增强了读写八种基本类型和字符串的功能。

增强了读写对象的功能:

readObject() 从流中读取一个对象。

writeObject() 向流中写入一个对象。

 1 package file.demo;
 2 
 3 import java.io.Serializable;
 4 
 5 /*序列化与反序列化注意事项:
 6 1.序列化类必须实现Serializable接口
 7 2.序列化类中的对象属性也要实现Serializable接口
 8 3.序列化版本号ID必须统一
 9 4.使用transient的属性不会被序列化
10 5.静态属性不会被序列化
11 6.序列化多个对象,可以通过ArrayList等集合类
12  */
13 public class Student implements Serializable {
14 
15     private static final long serialVersionUID = 100L;
16 
17     private String Name;
18     private Integer Age;//使用关键字: transient 修饰,就不会被序列化
19 
20     public String getName() {
21         return Name;
22     }
23 
24     public Integer getAge() {
25         return Age;
26     }
27 
28     public void setName(String name) {
29         Name = name;
30     }
31 
32     public void setAge(Integer age) {
33         Age = age;
34     }
35 
36     public Student(String name, Integer age) {
37         Name = name;
38         Age = age;
39     }
40 
41     @Override
42     public String toString() {
43         return "Student{" +
44                 "Name='" + Name + '\'' +
45                 ", Age=" + Age +
46                 '}';
47     }
48 }
 1 package file.demo;
 2 
 3 import java.io.FileOutputStream;
 4 import java.io.ObjectOutputStream;
 5 
 6 public class objectdemo01 {
 7     public static void main(String[] args) throws Exception {
 8         FileOutputStream fileOutputStream = new FileOutputStream("/Users/yangasen/Downloads/student.bin");
 9         ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
10 
11         objectOutputStream.writeObject(new Student("zhangsan", 18));
12 
13         objectOutputStream.close();
14     }
15 }
 1 package file.demo;
 2 
 3 import java.io.FileInputStream;
 4 import java.io.ObjectInputStream;
 5 
 6 public class objectdemo02 {
 7     public static void main(String[] args) throws Exception {
 8         FileInputStream fileInputStream = new FileInputStream("/Users/yangasen/Downloads/student.bin");
 9         ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
10 
11         Student student = (Student) objectInputStream.readObject();
12         System.out.println(student);
13 
14         objectInputStream.close();
15 
16     }
17 }

注意:

静态属性不能被序列化。

序列化多个对象,可以使用ArrayList。

posted on 2020-12-19 13:42  Sempron2800+  阅读(76)  评论(0编辑  收藏  举报