解决循环操作ObjectInputStream的readObject()方法的EOF异常
2016-04-01 17:48
531 查看
Code:
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class ObjectStream {
public static void main(String[] args) {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Person pe = null;
try {
FileOutputStream out = new FileOutputStream("D:/obj.txt");
oos = new ObjectOutputStream(out);
Person per = new Person("微软", 14);
pe = new Person("金山", 20);
oos.writeObject(per);
oos.writeObject(pe);
oos.writeObject(null);//解决EOF的关键,加入一个空的对象
System.out.println("添加成功");
oos.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try{
FileInputStream in = new FileInputStream("D:/obj.txt");
ois = new ObjectInputStream(in);
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class ObjectStream {
public static void main(String[] args) {
ObjectOutputStream oos = null;
ObjectInputStream ois = null;
Person pe = null;
try {
FileOutputStream out = new FileOutputStream("D:/obj.txt");
oos = new ObjectOutputStream(out);
Person per = new Person("微软", 14);
pe = new Person("金山", 20);
oos.writeObject(per);
oos.writeObject(pe);
oos.writeObject(null);//解决EOF的关键,加入一个空的对象
System.out.println("添加成功");
oos.close();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try{
FileInputStream in = new FileInputStream("D:/obj.txt");
ois = new ObjectInputStream(in);
相关文章推荐
- 执行命令行并等待完成(使用WaitForSingleObject达到目的)
- JSONObject转换JSON--将Date转换为指定格式
- SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-005Spring-Data-JPA例子的代码
- SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-004JPA例子的代码
- 使用 jackson ObjectMapper将java对象转换为json对象
- QT中的QMetaObject
- 设备模型之kobject,kset及其关系
- SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-003编写JPA-based repository( @PersistenceUnit、 @PersistenceContext、PersistenceAnnotationBeanPostProcessor)
- SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-002设置JPA的EntityManagerFactory(<persistence-unit>、<jee:jndi-lookup>)
- SPRING IN ACTION 第4版笔记-第十一章Persisting data with object-relational mapping-001-使用Hibernate(@Inject、@EnableTransactionManagement、@Repository、PersistenceExceptionTranslationPostProcessor)
- Android Studio Gradle Could not reserve enough space for object heap
- pragma mark 的使用
- 语言的开始
- 文章标题warning: could not load any Objective-C class information. This will significantly reduce the qu
- ./ggsci: error while loading shared libraries: libnnz11.so: cannot open shared object file: No such
- Effective Objective-C学习笔记 (持续更新)
- Objective-C之重载、重写、覆盖
- Objective-C的【同步机制】
- Objective-C中的深拷贝与浅拷贝
- 理解Objective-C消息转发机制