I/O操做总结(四))
前面已经把java io的主要操作讲完了
这一节我们来说说关于java io的其他内容
Serializable序列化
实例1:对象的序列化
|
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
|
import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.ObjectOutputStream;import java.io.Serializable;@SuppressWarnings("serial")//一个类要想实现序列化则必须实现Serializable接口class Person implements Serializable { private String name; private int age; public Person(String name, int age) { this.name = name; this.age = age; } public String toString() { return "Name:" + this.name + ", Age:" + this.age; }}public class Demo { public static void main(String[] args) { String path = File.separator + "home" + File.separator + "siu" + File.separator + "work" + File.separator + "demo.txt"; Person p1 = new Person("zhangsan",12); Person p2 = new Person("lisi",14); //此处创建文件写入流的引用是要给ObjectOutputStream的构造函数玩儿 FileOutputStream fos = null; ObjectOutputStream oos = null; try { fos = new FileOutputStream(path); oos = new ObjectOutputStream(fos); //这里可以写入对象,也可以写入其他类型数据 oos.writeObject(p1); oos.writeObject(p2); } catch (IOException e) { e.printStackTrace(); } finally { try { oos.close(); } catch (IOException e) { e.printStackTrace(); } } }} |
所谓对象序列化就是把一个对象进行持久化存储,方便保留其属性
通俗点说,等于把一个对象从堆内存里边揪出来放到硬盘上
当然,如果你开心,你可以序列化其他东西,包括数组,基本数据类型等等
来看看内容,神马玩意儿这是……

实例2:对象的反序列化
|
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
|
import java.io.File;import java.io.FileInputStream;import java.io.IOException;import java.io.ObjectInputStream;public class Demo { public static void main(String[] args) { String path = File.separator + "home" + File.separator + "siu" + File.separator + "work" + File.separator + "demo.txt"; //好吧,这里代码写得着实有点长了,还要抛异常什么的 //如果你也看的烦,那就在主方法上抛吧,构造方法里用匿名对象就好了 //什么?别告诉我你不知道匿名对象 FileInputStream fis = null; ObjectInputStream ois = null; try { fis = new FileInputStream(path); ois = new ObjectInputStream(fis); //这里返回的其实是一个Object类对象 //因为我们已知它是个Person类对象 //所以,就地把它给向下转型了 Person p = (Person)ois.readObject(); System.out.println(p); //抛死你,烦烦烦~!!! } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } finally { try { //还是要记得关闭下流 ois.close(); } catch (IOException e) { e.printStackTrace(); } } }} |
你看,我们把一个对象存放在硬盘上是为了方便日后使用
现在用得着它了,自然得拿出来

管道流
实例3:线程的通信
|
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
|
import java.io.IOException;import java.io.PipedInputStream;import java.io.PipedOutputStream;//实现Runnable接口,实现一个读的线程class Read implements Runnable { private PipedInputStream in; //将需要读的管道流传入到构造函数中 public Read(PipedInputStream in) { this.in = in; } //实现读这一线程 public void run() { try { byte[] buf = new byte[1024]; int temp = 0; //循环读取 //read是一个阻塞方法,需要抛异常 //此处把打印流的代码也加入进来 //是因为如果没有读取到数据,那么打印的代码也无效 while((temp = in.read(buf)) != -1) { String str = new String(buf,0,temp); System.out.println(str); } } catch (IOException e) { //其实这里应抛出一个自定义异常的 //暂时我还没弄清楚 e.printStackTrace(); } finally { try { //我已经抛火了,这只是为了提醒自己异常很重要 in.close(); } catch (IOException e) { e.printStackTrace(); } } } }//这里实现一个写的类class Write implements Runnable { private PipedOutputStream out; //将管道输入流传进来 public Write(PipedOutputStream out) { this.out = out; } public void run() { try { //这里开始写出数据 out.write("管道输出".getBytes()); } catch (IOException e) { e.printStackTrace(); } finally { try { //其实应该可以把这个关闭方法写到上面那个try里边 //但是这样感觉怪怪的,逻辑不大对 out.close(); } catch (IOException e) { e.printStackTrace(); } } }}public class Demo { public static void main(String[] args) { PipedInputStream in = new PipedInputStream(); PipedOutputStream out = new PipedOutputStream(); try { //连接管道 in.connect(out); //创建对象,开启线程 //此处同样放进try...catch里面 //因为如果没有链接管道,下面操作无意义 Read r = new Read(in); Write w = new Write(out); //把已经实现好run方法的对象放入线程中执行 new Thread(r).start(); new Thread(w).start(); } catch (IOException e) { e.printStackTrace(); } }} |
好吧,废了那么大劲儿,就打印了这么一句话,异常抛弃来很烦,为了注重细节……

管道流也许很难理解,其实非也
我们知道,字节流和字符流都需要数组来进行流的中转
而管道流则直接串联两条流,一边发送数据,一边接收
然而,同时通信的的两种状态,如何才能确定发送和接收的一致性呢
那么,就需要用到线程,无论是接收方还是发送方先执行
总会造成一个线程的阻塞状态,从而等待另一方的数据传过来
总体而言,管道流的目的,也就是为了线程通信
此外,还有PipedReader和PipedWriter类,操作原理都一样,这里就不再赘述了
DataOutputStream和DataInputStream类
实例4:基本数据类型的写入
|
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
|
import java.io.DataOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;public class Demo { public static void main(String[] args) { String path = File.separator + "home" + File.separator + "siu" + File.separator + "work" + File.separator + "demo.txt"; DataOutputStream d = null; try { //此处需要传入一个OutputStream类的对象 d = new DataOutputStream(new FileOutputStream(path)); //开始写入基本数据类型 d.writeInt(12); d.writeBoolean(true); d.writeDouble(12.2223); d.writeChar(97); //刷新流 d.flush(); } catch (IOException e) { e.printStackTrace(); } finally { try { d.close(); } catch (IOException e) { e.printStackTrace(); } } }} |
此处我们并不能直观看懂内容,因为它采用字节流的方式操作,而不是字符流
我们只需要知道,此程序已经将基本数据类型写入到硬盘即可

实例5:基本数据类型的读取
|
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
|
import java.io.DataInputStream;import java.io.File;import java.io.FileInputStream;import java.io.IOException;public class Demo { public static void main(String[] args) { String path = File.separator + "home" + File.separator + "siu" + File.separator + "work" + File.separator + "demo.txt"; DataInputStream d = null; try { d = new DataInputStream(new FileInputStream(path)); //按存储顺序读取基本数据类型 System.out.println(d.readInt()); System.out.println(d.readBoolean()); System.out.println(d.readDouble()); System.out.println(d.readChar()); } catch (IOException e) { e.printStackTrace(); } finally { try { d.close(); } catch (IOException e) { e.printStackTrace(); } } }} |
这里要注意的是,一定要按照写入顺序读取,否则会发生数据的打印错误

I/O操做总结(四))的更多相关文章
- Qt webKit可以做什么(四)--实现本地QObject和JavaScript交互
Qt webKit可以做什么(四)--实现本地QObject和JavaScript交互 Qt webKit可以做什么(四)--实现本地QObject和JavaScript交互
- java Class的Long id初始化 为0的问题android数据库操做出现的 android.database.sqlite.SQLiteConstraintException: error code 19: constraint failed
java的class中的Long类型变量调用默认的 构造函数new后会被初始化为0. 这句话大家可能感觉这么低级的事情还用你说? 我想说的是这个会产生的一个应用场景 和 避免方法 场景:db插入时候p ...
- Loadrunner教程--常用操做流程
1loadrunner压力测试一般使用流程 1.1loadrunner压力测试原理 本质就是在loadrunner上模拟多个用户同时按固定行为访问web站点.其中固定行为在loadrunner中是通过 ...
- I/O流操做总结(三)
说实话,其实我并不是很喜欢Java这门语言,尽管它很强大,有很多现成的API可以调用 但我总感觉它把简单的事情弄得太过复杂,甚至有时候会让人迷失 弄不清到底是为了写出东西,还是为了语言本身 我学习的第 ...
- I/O操做总结(二)
文件的操作 这一节我们来讨论关于文件自身的操作 不浪费唾沫了,用代码说话…… 实例1:创建文件对象 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 ...
- (转)Loadrunner教程--常用操做流程
1loadrunner压力测试一般使用流程 1.1loadrunner压力测试原理 本质就是在loadrunner上模拟多个用户同时按固定行为访问web站点.其中固定行为在loadrunner中是通过 ...
- dede网站安全要做的四件事
1,把安装install的文件夹删除:2,关闭member系统,禁止member文件写入:3,把data文件外移到网站根目录之外:4,把included和plus目录的写入权限关闭. 转载自:http ...
- javascript进阶修炼之一——javascript必备操做
动态选择方法及属性 使用方括号操作符,比点操作符功能更强大.因为可以在[ ]方括号中使用任何代表成员名称的内容访问对象.包括字面量,保存着成员名称的变量,名称组合,三元操作符.所有这些内容都会被处理成 ...
- TCP的三次握手和四次挥手与路由器(三层)转发原理
传输层是国际标准化组织提出的开放系统互连(OSI)参考模型中的第四层.该层协议为网络端点主机上的进程之间提供了可靠.有效的报文传送服务.其功能紧密地依赖于网络层的虚拟电路或数据报服务.传输层定义了主机 ...
随机推荐
- 闪回之 Flashback Query (dml表、过程、函数、包等)、Flashback version Query
Flashback Query 背景:Flashback 是 ORACLE 自 9i 就开始提供的一项特性,在 9i 中利用oracle 查询多版本一致的特点,实现从回滚段中读取表一定时间内操作过的数 ...
- POJ2253(djkstra求最长最短边)
Frogger Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32257 Accepted: 10396 Descrip ...
- web.xml中:<context-param>与<init-param>的区别与作用及获取方法
<context-param>的作用: web.xml的配置中<context-param>配置作用1. 启动一个WEB项目的时候,容器(如:Tomcat)会去读它的配置文件w ...
- font-size: 0;解决元素间的空白间隙
看别人的代码看到过font-size:0这个设置,不明白为何这样操作,后来研究一下才明白:这是像素级还原设计稿很有用的设置,因为元素节点有文本节点,在缩进代码时会占据宽度,这么说不好理解,演示如下: ...
- CI框架中,判断post,ajax,get请求的方法
CI框架当中并没有提供,类似tp框架中IS_POST,IS_AJAX,IS_GET的方法. 所有就得我们自己造轮子了.下面就介绍一下,如何定义这些判断请求的方法.其实很简单的. 首先打开constan ...
- POJ1251 Jungle Roads Kruskal+scanf输入小技巧
Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai ...
- Python开发【第四篇】:运算符
1. 算术运算符 算术运算符包括+.-.*./.%.//.**,即加减乘除,取余,取商(地板除法)以及幂运算. >>> 5 + 2 7 >>> 5 - 2 ...
- std::function"函数"对象包装器
语义: 类模板std::function是可调用对象的包装器,可以包装除了类成员之外的所有可调用对象.包括,普通函数,函数指针,lambda,仿函数.通过指定的模板参数,它可以用统一的方式保存,并延迟 ...
- JAVA之动态编译
通过Java动态生成class文件 今天说下JAVA中的动态编译,这个功能根据我现在的了解好像没有见到过用的,我Jio的吧,现在的一些在线代码编缉器可以用到了,这个具体我也不是很清楚.感兴趣的大家可以 ...
- 9.Python初窥门径(函数初识)
Python(函数初识) 一.初识函数 面向过程缺点 : 代码重复,代码描述性不高 函数 : 函数是以功能为导向,一个函数封装一个功能. 函数的优点 : 减少代码重复性,增强了代码的可读性 二.函数的 ...