解决ObjectInputStream的readObject()方法的EOF异常
2015-10-21 15:48
453 查看
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();
}
}
class Person implements Serializable {
private String name;
private /transient/ int age;
}
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); Object obj=null; while((obj=ois.readObject())!=null) { Person s = (Person)obj; System.out.println(s); } ois.close(); in.close(); } catch(Exception e) { e.printStackTrace(); } }
}
class Person implements Serializable {
private String name;
private /transient/ int age;
public Person(String name, int age) { this.name = name; this.age = age; } public String getNmae(){ return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String toString() { return "姓名:" + name + " 性别:" + age; }
}
相关文章推荐
- 读effective objective-c 2.0 整理文章 - 41条 多用派发队列,少用锁
- 跟我学Guava:基础1.3-常见Object方法和复用他们
- Object-C 中各数据类型转换 NSData转NSString,Byte,UIImage
- WINDOWS下,中文JSON格式读取报错处理:ValueError: No JSON object could be decoded
- [MSSQL]SQL Server OBJECT_ID用法-判断对象是否存在
- 常用的Objective-C第三方库(更新中)
- 使用vectorizer.fit_transform时出现AttributeError: 'file' object has no attribute 'lower'
- Swift与Objective-C
- Windows下的Objective-C集成开发环境(IDE)
- 【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词
- 【IOS 开发】Objective-C Foundation 框架 -- 字符串 | 日期 | 对象复制 | NSArray | NSSet | NSDictionary | 谓词
- [Objective-C]OC中字典基本概念和常用方法(NSDictionary和NSMutableDictionary)
- Java总结篇系列:java.lang.Object
- AsyncTask java.lang.ClassCastException: java.lang.Object[] cannot be cast to java.lang.Void[] 异常
- Xcode7编译遇到building for iOS simulator, but linking in object file built for OSX, for architectu问题解决方案
- Objective-C Foundation框架实践——NSNumber(一)
- An entity object cannot be referenced by multiple instances of IEntityChangeTracker.
- Object类型数据转换成Long类型数据
- 简单图书管理实现
- 常见的Objective-C笔试题