1 ---------------------------------------------------I/O-------------------------------------------------
2 源数据源----->程序------>目标数据源
3 (输入流)读入 (输出流)读出
4 输入输出流是相对计算机内存而言
5
6
7 1.按流向划分
8 输出流
9 OutputStream,Writer
10 输入流
11 InputStream,Reader
12
13 2.按处理数据单元划分
14 ----------------------------字节流--------------------------
15 1. 输入流
16 (基类)InputStream
17 方法:int read() 返回值是字节对应的整型数字
18 int read(byte[] b) 返回值是字节的个数(b的长度)
19 int read(byte[] b,int off,int len) 将读取的第一个字节存储在元素b[off]中
20 void close()
21 int available() 可以从输入流中读取的字节数目
22 (子类)FileInputStream
23 方法:FileInputStream(File file)
24 FileInputStream(String name)
25 实现步骤
26 引入相关类-->构造文件输入流,FileInputStream对象-->读取文本文件的数据-->关闭文件流对象
27
28 2. 输出流 ★★string变成getByte变成字节★★
29 (基类)OutputStream
30 方法:int write()
31 int write(byte[] b)
32 int write(byte[] b,int off,int len)
33 void close()
34 void flush() 强制把缓冲区的数据写到输出流中
35 (子类)FileOutputStream ★如果相应的文件不存在,自动创建一个空的文件★
36 方法:FileOutputStream(File file)
37 FileOutputStream(String name)
38 FileOutputStream(String name,boolean append) 覆盖原来的内容
39 实现步骤
40 引入相关类-->构造文件输出流,FileOutputStream对象-->把数据写入文本文件-->关闭文件流对象
41
42 --------------------------字符流-------------------------------
43 1. 输入流
44 (基类)Reader
45 (子类)InputStreamReader
46 方法:InputStreamReader(InputStream in)
47 InputStreamReader(InputStream in,String charsetName) 编码格式
48 (孙子类)FileReader ★★只能按照本地平台默认的字符编码来读★★
49 方法:FileReader(File file)
50 FileReader(String pathName,boolean append)
51 ◆◆◆ (孙子类2)BufferedReader
52 实现步骤
53 引入相关类-->构造BufferedReader和FileReader对象-->调用read()方法读取数据-->流对象关闭close()
54
55 2. 输出流
56 (基类)Writer
57 (子类)OutputStreamWriter
58 方法:OutputStreamWriter(OutputStream in)
59 OutputStreamWriter(OutputStream in,String charsetName) 编码格式
60 (孙子类)FileWriter ★★只能按照本地平台默认的字符编码来读★★
61 本地平台的字符编码获得:system.getProperty("file.encoding");
62 方法:FileWriter(File file)
63 FileWriter(String pathName,boolean append)
64 ◆◆◆ (孙子类2)BufferedWriter
65 实现步骤
66 引入相关类-->构造BufferedWriter和FileWriter对象-->调用write()方法写数据-->流对象的清空和关闭flush()和close()
67
68
69
70 -------------------------序列化和反序列化--------------------
71 序列化:将对象的状态写到特定的流中的过程
72 反序列化:从特定的流中获取数据重新构建对象的过程
73
74 如果对象可以实现实现Serializable接口,那么该对象可以序列化或反序列化
75 序列化
76 步骤:
77 实现Serializable接口--->创建对象输出流--->调用writeObject()方法将对象写入文件--->关闭对象输出流
78 ObjectOutputStream
79
80 反序列化
81 步骤:
82 实现Serializable接口--->创建对象输入流--->调用readObject()方法读取对象--->关闭对象输入流
83 ObjectInputStream
84
85 范例:
86 public class Student implements Serializable{
87 private int id;
88 private int age;
89 private String name;
90 //当不想序列化属性时,可以加上transient
91 private transient String password;
92
93 public Student() {}
94
95 public Student(int id, int age, String name, String password) {
96 super();
97 this.id = id;
98 this.age = age;
99 this.name = name;
100 this.password = password;
101 }
102
103 public int getId() {
104 return id;
105 }
106 public void setId(int id) {
107 this.id = id;
108 }
109 public int getAge() {
110 return age;
111 }
112 public void setAge(int age) {
113 this.age = age;
114 }
115 public String getName() {
116 return name;
117 }
118 public void setName(String name) {
119 this.name = name;
120 }
121 public String getPassword() {
122 return password;
123 }
124
125 public void setPassword(String password) {
126 this.password = password;
127 }
128 }
129
130 public class ObjectSerDemo {
131 public static void main(String[] args) {
132 FileOutputStream fos = null;
133 ObjectOutputStream oos = null;
134 ObjectInputStream ois = null;
135 try {
136 //创建对象序列化需要的输出流
137 fos = new FileOutputStream("E:\\Java实训课总结\\2016.3.26\\stu.txt");
138 oos = new ObjectOutputStream(fos);
139 //创建对象
140 Student stu = new Student(1,18,"张三","12345");
141 //序列化对象
142 oos.writeObject(stu);
143 //反序列化
144 ois = new ObjectInputStream(new FileInputStream("E:\\Java实训课总结\\2016.3.26\\stu.txt"));
145 Student stu1 = (Student)ois.readObject();
146 System.out.println("反序列化后:" );
147 System.out.println("学号:" + stu1.getId());
148 System.out.println("年龄:" + stu1.getAge());
149 System.out.println("姓名:" + stu1.getName());
150 System.out.println("密码:" + stu1.getPassword());
151 } catch (FileNotFoundException e) {
152 e.printStackTrace();
153 } catch (IOException e) {
154 e.printStackTrace();
155 } catch (ClassNotFoundException e) {
156 e.printStackTrace();
157 } finally {
158 //关闭流
159 try {
160 oos.close();
161 fos.close();
162 ois.close();
163 } catch (IOException e) {
164 e.printStackTrace();
165 }
166 }
167 }
168 }