Fork me on GitHub

二十二.IO流

文章博主觉得重在使用

一.基本概念和IO入门

对于任何程序设计语言而言,输入输出(Input/Output)系统都是非常核心的功能。程序运行需要数据,数据的获取往往需要跟外部系统进行通信,外部系统可能是文件、数据库、其他程序、网络、IO设备等等。外部系统比较复杂多变,那么我们有必要通过某种手段进行抽象、屏蔽外部的差异,从而实现更加便捷的编程。

输入(Input)指的是:可以让程序从外部系统获得数据(核心含义是“读”,读取外部数据)。常见的应用:

  • 读取硬盘上的文件内容到程序。例如:播放器打开一个视频文件、word打开一个doc文件。
  • 读取网络上某个位置内容到程序。例如:浏览器中输入网址后,打开该网址对应的网页内容;下载网络上某个网址的文件。
  • 读取数据库系统的数据到程序。
  • 读取某些硬件系统数据到程序。例如:车载电脑读取雷达扫描信息到程序;温控系统等。

输出(Output)指的是:程序输出数据给外部系统从而可以操作外部系统(核心含义是“写”,将数据写出到外部系统)。常见的应用有:

  • 将数据写到硬盘中。例如:我们编辑完一个word文档后,将内容写到硬盘上进行保存。
  • 将数据写到数据库系统中。例如:我们注册一个网站会员,实际就是后台程序向数据库中写入一条记录。
  • 将数据写到某些硬件系统中。例如:导弹系统导航程序将新的路径输出到飞控子系统,飞控子系统根据数据修正飞行路径。
  • java.io包为我们提供了相关的API,实现了对所有外部系统的输入输出操作

二.数据源

数据源data source,提供数据的原始媒介。常见的数据源有:数据库、文件、其他程序、内存、网络连接、IO设备。

数据源分为:源设备、目标设备。

  1. 源设备:为程序提供数据,一般对应输入流。
  2. 目标设备:程序数据的目的地,一般对应输出流。

image

三.流的概念

流是一个抽象、动态的概念,是一连串连续动态的数据集合。

对于输入流而言,数据源就像水箱,流(stream)就像水管中流动着的水流,程序就是我们最终的用户。我们通过流(A Stream)将数据源(Source)中的数据(information)输送到程序(Program)中。

对于输出流而言,目标数据源就是目的地(dest),我们通过流(A Stream)将程序(Program)中的数据(information)输送到目的数据源(dest)中。

image

注意:输入/输出流的划分是相对程序而言的,并不是相对数据源。

四.Java中流的概念细分

按流的方向分类:

  1. 输入流:数据流向是数据源到程序(以InputStream、Reader结尾的流)。
  2. 输出流:数据流向是程序到目的地(以OutPutStream、Writer结尾的流)。

image

按处理的数据单元分类:

  1. 字节流:以字节为单位获取数据,命名上以Stream结尾的流一般是字节流,如FileInputStream、FileOutputStream。
  2. 字符流:以字符为单位获取数据,命名上以Reader/Writer结尾的流一般是字符流,如FileReader、FileWriter。

按处理对象不同分类:

  1. 节点流:可以直接从数据源或目的地读写数据,如FileInputStream、FileReader、DataInputStream等。
  2. 处理流:不直接连接到数据源或目的地,是”处理流的流”。通过对其他流的处理提高程序的性能,如BufferedInputStream、BufferedReader等。处理流也叫包装流。

节点流处于IO操作的第一线,所有操作必须通过它们进行;处理流可以对节点流进行包装,提高性能或提高程序的灵活性。

image

五.Java中IO流类的体系

image

从上图发现,很多流都是成对出现的,比如:FileInputStream/FileOutputStream,显然是对文件做输入和输出操作的。我们下面简单做个总结:

  1. InputStream/OutputStream——->字节流的抽象类。
  2. Reader/Writer———>字符流的抽象类。
  3. FileInputStream/FileOutputStream——–>节点流:以字节为单位直接操作“文件”。
  4. ByteArrayInputStream/ByteArrayOutputStream———>节点流:以字节为单位直接操作“字节数组对象”。
  5. ObjectInputStream/ObjectOutputStream———>处理流:以字节为单位直接操作“对象”。
  6. DataInputStream/DataOutputStream———>处理流:以字节为单位直接操作“基本数据类型与字符串类型”。
  7. FileReader/FileWriter———>节点流:以字符为单位直接操作“文本文件”(注意:只能读写文本文件)。
  8. BufferedReader/BufferedWriter———>处理流:将Reader/Writer对象进行包装,增加缓存功能,提高读写效率。
  9. BufferedInputStream/BufferedOutputStream———->处理流:将InputStream/OutputStream对象进行包装,增加缓存功能,提高 读写效率。
  10. InputStreamReader/OutputStreamWriter———->处理流:将字节流对象转化成字符流对象。
  11. PrintStream————–>处理流:将OutputStream进行包装,可以方便地输出字符,更加灵活。

六.四大IO抽象类

image

七.文件字节流

FileInputStream通过字节的方式读取文件,适合读取所有类型的文件(图像、视频、文本文件等)。Java也提供了FileReader专门读取文本文件。

FileOutputStream 通过字节的方式写数据到文件中,适合所有类型的文件。Java也提供了FileWriter专门写入文本文件。

FileInputStream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package yp.JavaSE.Review_10_IO流;

import java.io.*;

/**
* @author RickYinPeng
* @ClassName Test_08_IO
* @Description 文件字节输入流
* @date 2019/1/25/12:59
*/
public class Test_08_IO {
/**
* 1:创建源
* 2:选择流
* 3:操作
* 4:释放资源
* @param args
*/
public static void main(String[] args) {
/**
* 1:创建源
*/
File src = new File("gg.txt");
InputStream inputStream = null;
try {
/**
* 2:选择流
*/
inputStream = new FileInputStream(src);
/**
* 循环读操作
*/
byte[] flush = new byte[1024];//缓冲容器 1024字节==1k
int len = -1;//接受长度
while ((len=inputStream.read(flush))!=-1){
/**
* 字节数组--->字符串
*/
String str = new String(flush,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
/**
* 4:释放资源
*/
try {
if(null!=inputStream){
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

FileOutputStream

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package yp.JavaSE.Review_10_IO流;

import java.io.*;

/**
* @author RickYinPeng
* @ClassName Test_09_FileOutPutStream
* @Description 文件字节输出流
* @date 2019/1/25/13:27
*/
public class Test_09_FileOutPutStream {
/**
* 1:创建源
* 2:选择流
* 3:操作,写出内容
* 4:释放资源
* @param args
*/
public static void main(String[] args) {
/**
* 1:创建流,如果不存在会帮你创建,而InputStream必须存在,因为你是读取文件啊
*/
File desc = new File("desc.txt");

/**
* 2:选择流
*/
OutputStream os = null;

try {
/**
* append参数:
* true:将当前所写的内容追加到desc文件的后面
* false:将当前所写的内容覆盖之前的内容
*/
os = new FileOutputStream(desc,false);

/**
* 3:操作
*/
String msg = "asdasdsad";

//编码
byte[] datas = msg.getBytes();

os.write(datas,0,datas.length);

/**
* 避免我们的数据驻留在内存
*/
os.flush();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
/**
* 4:释放资源
*/
try {
if(null!=os){
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}

}
}

使用FileOutputStream和FileInputStream实现文件拷贝

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package yp.JavaSE.Review_10_IO流;

import java.io.*;

/**
* @author RickYinPeng
* @ClassName Test_10_Copy
* @Description 文件拷贝
* @date 2019/1/25/13:43
*/
public class Test_10_Copy {
public static void main(String[] args) {
/**
* 源头
*/
File src = new File("gg.txt");
/**
* 拷贝的目的地
*/
File desc = new File("desc.txt");

OutputStream os = null;
InputStream is = null;

try {
is = new FileInputStream(src);
os = new FileOutputStream(desc);

byte[] flush = new byte[1024];
int len = -1;
while ((len = is.read(flush))!=-1){
os.write(flush,0,len);
}
os.flush();


} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
/**
* 释放资源:先打开的后关闭
*/
//先使用到输入流所以后关闭输入流
if(null!=os){
os.close();
}
if(null!=is){
is.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

}

在使用文件字节流时,我们需要注意以下两点:

  1. 为了减少对硬盘的读写次数,提高效率,通常设置缓存数组。相应地,读取时使用的方法为:read(byte[] b);写入时的方法为:write(byte[ ] b, int off, int length)。
  2. 程序中如果遇到多个流,每个流都要单独关闭,防止其中一个流出现异常后导致其他流无法关闭的情况。

八.文件字符流

面介绍的文件字节流可以处理所有的文件,但是字节流不能很好的处理Unicode字符,经常会出现“乱码”现象。所以,我们处理文本文件,一般可以使用文件字符流,它以字符为单位进行操作。

FileReader

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package yp.JavaSE.Review_10_IO流;

import java.io.*;

/**
* @author RickYinPeng
* @ClassName Test_11_FileRead
* @Description 文件输入字符流
* @date 2019/1/25/15:46
*/
public class Test_11_FileRead {
/**
* 1:创建源
* 2:选择流
* 3:操作
* 4:释放资源
* @param args
*/
public static void main(String[] args) {
/**
* 1:创建源
*/
File src = new File("desc.txt");

Reader reader = null;

try {
/**
* 2:选择流
*/
reader = new FileReader(src);

/**
* 3:操作:注意:之前我们的字节流操作的是Byte数组,而这里操作的是我们的字符数组
*/
char[] flush = new char[1];
int len = -1;

while ((len = reader.read(flush))!=-1){
/**
* 字符数组---->字符串(不存在解码)
*/
String str = new String(flush,0,len);
System.out.println(str);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

FileWriter

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package yp.JavaSE.Review_10_IO流;

import java.io.*;

/**
* @author RickYinPeng
* @ClassName Test_11_FileRead
* @Description 文件输出字符流
* @date 2019/1/25/15:46
*/
public class Test_11_FileWriter {
/**
* 1:创建源
* 2:选择流
* 3:操作
* 4:释放资源
*
* @param args
*/
public static void main(String[] args) {
/**
* 1:创建源
*/
File src = new File("desc.txt");

Writer writer = null;

try {
/**
* 2:选择流
*/
writer = new FileWriter(src);

/**
* 3:操作:注意:之前我们的字节流操作的是Byte数组,而这里操作的是我们的字符数组
*/

/**
* 写法一:
*/
/* String msg = "你好啊,傻逼";
writer.write(msg);
writer.flush();*/

/**
* 写法二:
*/
/* String msg = "你好啊,傻逼";
char[] datas = msg.toCharArray();
writer.write(datas,0,datas.length);
writer.flush();*/

/**
* 写法三:
*/
writer.append("asdasd").append("你好啊世界");
writer.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

九.字节数组流(内存流)

字节数组输入流

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package yp.JavaSE.Review_10_IO流;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
* @author RickYinPeng
* @ClassName Test_12_ByteArrayInputStream
* @Description 字节数组输入流
* @date 2019/1/25/16:28
*/
public class Test_12_ByteArrayInputStream {
public static void main(String[] args) {
/**
* 1:创建源:字节数组,不要太大
*/
byte[] src = "talk is cheap show me the code".getBytes();

/**
* 2:选择流
*/
InputStream is = null;

try {
is = new ByteArrayInputStream(src);

/**
* 3:操作
*/
byte[] flush = new byte[1024];
int len = -1;
while ((len = is.read(flush))!=-1){
String str = new String(flush,0,len);
System.out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
/**
* close方法这里可以不用调
*/
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
}

字节数组输出流

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package yp.JavaSE.Review_10_IO流;

import java.io.*;

/**
* @author RickYinPeng
* @ClassName Test_09_FileOutPutStream
* @Description 字节数组输出流
* @date 2019/1/25/13:27
*/
public class Test_12_ByteArrayOutputStream {
/**
* 1:创建源
* 2:选择流
* 3:操作,写出内容
* 4:释放资源
* @param args
*/
public static void main(String[] args) {
/**
* 1:创建源
*/
byte[] desc = null;

/**
* 2:选择流(新增方法)
*/
ByteArrayOutputStream bao = null;

try {
bao = new ByteArrayOutputStream();

/**
* 3:操作
*/
String msg = "show me the code";

//编码
byte[] datas = msg.getBytes();

bao.write(datas,0,datas.length);

/**
* 避免我们的数据驻留在内存
*/
bao.flush();

/**
* 获取数据
*/
desc = bao.toByteArray();
System.out.println(new String(desc,0,desc.length));
System.out.println(new String(datas,0,datas.length));

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
/**
* 4:释放资源
*/
try {
bao.close();
} catch (IOException e) {
e.printStackTrace();
}
}

}
}

将图片读取到字节数组中

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package yp.JavaSE.Review_10_IO流;

import java.io.*;

/**
* @author RickYinPeng
* @ClassName Test_13_image
* @Description 将图片读取到字节数组中
* @date 2019/1/25/16:49
*/
public class Test_13_image {
public static void main(String[] args) throws FileNotFoundException {
byte[] bytes = fileToByteArray("1.png");
byteArrayToFile(bytes,"a.png");
}

/**
* 将图片读取到字节数组
* 1:图片到程序:FileInputStream
* 2:程序到字节数组:ByteArrayOutputStream
*/
public static byte[] fileToByteArray(String filePath){
/**
* 1:创建数据源和目的地
*/
File src = new File(filePath);
byte[] desc = null;

InputStream inputStream = null;
ByteArrayOutputStream baos = null;
try {
/**
* 2:选择流
*/
inputStream = new FileInputStream(src);
baos = new ByteArrayOutputStream();

/**
* 循环读操作
*/
byte[] flush = new byte[1024];//缓冲容器 1024字节==1k
int len = -1;//接受长度
while ((len=inputStream.read(flush))!=-1){
/**
* 写出到字节数组中
*/
baos.write(flush,0,len);
}
baos.flush();
return baos.toByteArray();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
/**
* 4:释放资源
*/
try {
if(null!=inputStream){
inputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}

/**
* 将字节数组写出到图片
* 1:字节数组写出到图片:ByteArrayInputStream
* 2:程序写出到文件:FileOutputStream
*/
public static void byteArrayToFile(byte[] src,String filePath) throws FileNotFoundException {
/**
* 创建源
*/
File desc = new File(filePath);

/**
* 2:选择流
*/
InputStream is = null;
OutputStream os = null;

try {
is = new ByteArrayInputStream(src);
os = new FileOutputStream(desc);

/**
* 3:操作
*/
byte[] flush = new byte[1024];
int len = -1;
while ((len = is.read(flush))!=-1){
os.write(flush,0,len);
}
os.flush();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
/**
* close方法这里可以不用调
*/
is.close();
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

面试题

除了使用String转成Byte数组外还有什么方法?

可以使用我上面说的这种内存流

十.数据流

数据流将“基本数据类型与字符串类型”作为数据源,从而允许程序以与机器无关的方式从底层输入输出流中操作Java基本数据类型与字符串类型。

DataInputStream和DataOutputStream提供了可以存取与机器无关的所有Java基础类型数据(如:int、double、String等)的方法。

DataInputStream和DataOutputStream是处理流,可以对其他节点流或处理流进行包装,增加一些更灵活、更高效的功能。

DataInputStream和DataOutputStream的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class TestDataStream {
public static void main(String[] args) {
DataOutputStream dos = null;
DataInputStream dis = null;
FileOutputStream fos = null;
FileInputStream fis = null;
try {
fos = new FileOutputStream("D:/data.txt");
fis = new FileInputStream("D:/data.txt");
//使用数据流对缓冲流进行包装,新增缓冲功能
dos = new DataOutputStream(new BufferedOutputStream(fos));
dis = new DataInputStream(new BufferedInputStream(fis));
//将如下数据写入到文件中
dos.writeChar('a');
dos.writeInt(10);
dos.writeDouble(Math.random());
dos.writeBoolean(true);
dos.writeUTF("北京尚学堂");
//手动刷新缓冲区:将流中数据写入到文件中
dos.flush();
//直接读取数据:读取的顺序要与写入的顺序一致,否则不能正确读取数据。
System.out.println("char: " + dis.readChar());
System.out.println("int: " + dis.readInt());
System.out.println("double: " + dis.readDouble());
System.out.println("boolean: " + dis.readBoolean());
System.out.println("String: " + dis.readUTF());
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if(dos!=null){
dos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(dis!=null){
dis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fos!=null){
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if(fis!=null){
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
注意:使用数据流时,读取的顺序一定要与写入的顺序一致,否则不能正确读取数据。

## 十一.对象流

我们前边学到的数据流只能实现对基本数据类型和字符串类型的读写,并不能读取对象(字符串除外),如果要对某个对象进行读写操作,我们需要学习一对新的处理流:ObjectInputStream/ObjectOutputStream。

ObjectInputStream/ObjectOutputStream是以“对象”为数据源,但是必须将传输的对象进行序列化与反序列化操作。

ObjectInputStream/ObjectOutputStream的使用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.Date;

public class TestObjectStream {
public static void main(String[] args) throws IOException, ClassNotFoundException {
write();
read();
}
/**使用对象输出流将数据写入文件*/
public static void write(){
// 创建Object输出流,并包装缓冲流,增加缓冲功能
OutputStream os = null;
BufferedOutputStream bos = null;
ObjectOutputStream oos = null;
try {
os = new FileOutputStream(new File("d:/bjsxt.txt"));
bos = new BufferedOutputStream(os);
oos = new ObjectOutputStream(bos);
// 使用Object输出流
//对象流也可以对基本数据类型进行读写操作
oos.writeInt(12);
oos.writeDouble(3.14);
oos.writeChar('A');
oos.writeBoolean(true);
oos.writeUTF("北京尚学堂");
//对象流能够对对象数据类型进行读写操作
//Date是系统提供的类,已经实现了序列化接口
//如果是自定义类,则需要自己实现序列化接口
oos.writeObject(new Date());
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭输出流
if(oos != null){
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bos != null){
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(os != null){
try {
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**使用对象输入流将数据读入程序*/
public static void read() {
// 创建Object输入流
InputStream is = null;
BufferedInputStream bis = null;
ObjectInputStream ois = null;
try {
is = new FileInputStream(new File("d:/bjsxt.txt"));
bis = new BufferedInputStream(is);
ois = new ObjectInputStream(bis);
// 使用Object输入流按照写入顺序读取
System.out.println(ois.readInt());
System.out.println(ois.readDouble());
System.out.println(ois.readChar());
System.out.println(ois.readBoolean());
System.out.println(ois.readUTF());
System.out.println(ois.readObject().toString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭Object输入流
if(ois != null){
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(bis != null){
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(is != null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}

十二.转换流

InputStreamReader/OutputStreamWriter用来实现将字节流转化成字符流。

System.in是字节流对象,代表键盘的输入,如果我们想按行接收用户的输入时,就必须用到缓冲字符流BufferedReader特有的方法readLine(),但是经过观察会发现在创建BufferedReader的构造方法的参数必须是一个Reader对象,这时候我们的转换流InputStreamReader就派上用场了。

而System.out也是字节流对象,代表输出到显示器,按行读取用户的输入后,并且要将读取的一行字符串直接显示到控制台,就需要用到字符流的write(String str)方法,所以我们要使用OutputStreamWriter将字节流转化为字符流。

### 使用InputStreamReader接收用户的输入,并输出到控制台

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class TestConvertStream {
public static void main(String[] args) {
// 创建字符输入和输出流:使用转换流将字节流转换成字符流
BufferedReader br = null;
BufferedWriter bw = null;
try {
br = new BufferedReader(new InputStreamReader(System.in));
bw = new BufferedWriter(new OutputStreamWriter(System.out));
// 使用字符输入和输出流
String str = br.readLine();
// 一直读取,直到用户输入了exit为止
while (!"exit".equals(str)) {
// 写到控制台
bw.write(str);
bw.newLine();// 写一行后换行
bw.flush();// 手动刷新
// 再读一行
str = br.readLine();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
// 关闭字符输入和输出流
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (bw != null) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}


## 十三.序列化和反序列化

### 13.1 序列化和反序列化是什么

当两个进程远程通信时,彼此可以发送各种类型的数据。 无论是何种类型的数据,都会以二进制序列的形式在网络上传送。比如,我们可以通过http协议发送字符串信息;我们也可以在网络上直接发送Java对象。发送方需要把这个Java对象转换为字节序列,才能在网络上传送;接收方则需要把字节序列再恢复为Java对象才能正常读取。

把Java对象转换为字节序列的过程称为对象的序列化。把字节序列恢复为Java对象的过程称为对象的反序列化。

对象序列化的作用有如下两种:
1. 持久化: 把对象的字节序列永久地保存到硬盘上,通常存放在一个文件中,比如:休眠的实现。以后服务器session管理,hibernate将对象持久化实现。
2. 网络通信:在网络上传送对象的字节序列。比如:服务器之间的数据通信、对象传递。

### 13.2 序列化涉及的类和接口

ObjectOutputStream代表对象输出流,它的writeObject(Object obj)方法可对参数指定的obj对象进行序列化,把得到的字节序列写到一个目标输出流中。

ObjectInputStream代表对象输入流,它的readObject()方法从一个源输入流中读取字节序列,再把它们反序列化为一个对象,并将其返回。

只有实现了Serializable接口的类的对象才能被序列化。 Serializable接口是一个空接口,只起到标记作用。

13.3 序列化/反序列化的步骤和实例

将Person类的实例进行序列化和反序列化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

//Person类实现Serializable接口后,Person对象才能被序列化
class Person implements Serializable {
// 添加序列化ID,它决定着是否能够成功反序列化!
private static final long serialVersionUID = 1L;
int age;
boolean isMan;
String name;

public Person(int age, boolean isMan, String name) {
super();
this.age = age;
this.isMan = isMan;
this.name = name;
}

@Override
public String toString() {
return "Person [age=" + age + ", isMan=" + isMan + ", name=" + name + "]";
}
}

public class TestSerializable {
public static void main(String[] args) {
FileOutputStream fos = null;
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
FileInputStream fis = null;
try {
// 通过ObjectOutputStream将Person对象的数据写入到文件中,即序列化。
Person person = new Person(18, true, "高淇");
// 序列化
fos = new FileOutputStream("d:/c.txt");
oos = new ObjectOutputStream(fos);
oos.writeObject(person);
oos.flush();
// 反序列化
fis = new FileInputStream("d:/c.txt");
// 通过ObjectInputStream将文件中二进制数据反序列化成Person对象:
ois = new ObjectInputStream(fis);
Person p = (Person) ois.readObject();
System.out.println(p);
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
注意:
1. static属性不参与序列化。
2. 对象中的某些属性如果不想被序列化,不能使用static,而是使用transient修饰。
3. 为了防止读和写的序列化ID不一致,一般指定一个固定的序列化ID。

总结:

  1. 按流的方向分类:

    输入流:数据源到程序(InputStream、Reader读进来)。

    输出流:程序到目的地(OutPutStream、Writer写出去)。

  2. 按流的处理数据单元分类:

    字节流:按照字节读取数据(InputStream、OutputStream)。

    字符流:按照字符读取数据(Reader、Writer)。

  3. 按流的功能分类:

    节点流:可以直接从数据源或目的地读写数据。

    处理流:不直接连接到数据源或目的地,是处理流的流。通过对其他流的处理提高程序的性能。

  4. IO的四个基本抽象类:InputStream、OutputStream、Reader、Writer

  5. InputStream的实现类:

    FileInputStream

    ByteArrayInutStream

    BufferedInputStream

    DataInputStream

    ObjectInputStream

  6. OutputStream的实现类:

    FileOutputStream

    ByteArrayOutputStream

    BufferedOutputStream

    DataOutputStream

    ObjectOutputStream

    PrintStream

  7. Reader的实现类

    FileReader

    BufferedReader

    InputStreamReader

  8. Writer的实现类

    FileWriter

    BufferedWriter

    OutputStreamWriter

  9. 把Java对象转换为字节序列的过程称为对象的序列化。

  10. 把字节序列恢复为Java对象的过程称为对象的反序列化。

image