刚开始的时候我以为:ObjectOutputStream和ObjectInputStream类的文件的输入输出流只能操作集合或者数组:比如:
public class Userfile {
public static int outObjecStream(File file, List
FileOutputStream fileStream = null;
ObjectOutputStream objStream = null;
try {
fileStream = new FileOutputStream(file,false);
objStream = new ObjectOutputStream(fileStream);
objStream.writeObject(list);
objStream.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
finally{
try {
fileStream.close();
objStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return 0;
}
public static ArrayList
FileInputStream inStream = null;
ObjectInputStream objStream = null;
ArrayList
User User2 = null;
try {
inStream = new FileInputStream(file);
objStream = new ObjectInputStream(inStream);
Users.addAll((ArrayList
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
finally{
try {
inStream.close();
objStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return Users;
}
}
但是经过仔细的推敲,其实ObjectOutputStream和ObjectInputStream类也是可以操作单个的对象的:
User对象自己定义
public class ObjectSeriableTest {
public static void main(String[] args) throws IOException {
// 对象的序列化 把对象变为文件
File file = new File("d:\\user.txt");
// User user = new User("1", "test", "123456");
// User user1 = new User("2", "test1", "123456");
// User user2 = new User("3", "test2", "123456");
// writByObjectOutput(file, user);
// writByObjectOutput(file, user1);
// writByObjectOutput(file, user2);
// 对象的反序列化 从文件变为对象
// User userread=getByObjectObjectInput(file);
// System.out.println(userread.getId()+":"+userread.getName());
// List
// getByObjectObjectInput(file, objects);
// for (User obj : objects) {
//
// System.out.println(obj.getId() + ":" + obj.getName());
// }
// ObjectInput
// List
// for (User user : users) {
// System.out.println(user.getId() + ":" + user.getName());
// }
// 存储集合对象
File file1 = new File("d:\\user1.txt");
// List
// users1.add(new User("1", "ddd", "123232"));
// users1.add(new User("2", "ddd1", "123232"));
// users1.add(new User("3", "ddd2", "123232"));
//
// writeList(file1, users1);
List
for(User user:users2)
{
System.out.println(user.getId() + ":" + user.getName());
}
}
public static Object readList(File file) {
InputStream iStream = null;
ObjectInputStream oInstream = null;
try {
iStream = new FileInputStream(file);
oInstream = new ObjectInputStream(iStream);
// oInstream.readObject();
Object obj = oInstream.readObject();
return obj;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static void writeList(File file, Object obj) {
OutputStream oStream = null;
ObjectOutputStream oOutStream = null;
try {
oStream = new FileOutputStream(file, true);
if (file.length() > 1) {
oOutStream = new MyObjectOutputStream(oStream);
} else {
oOutStream = new ObjectOutputStream(oStream);
}
oOutStream.writeObject(obj);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
oStream.close();
oOutStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public static void getByObjectObjectInput(File file,
List extends Serializable> lists) {
InputStream iStream = null;
List
ObjectInputStream oInstream = null;
try {
iStream = new FileInputStream(file);
oInstream = new ObjectInputStream(iStream);
// oInstream.readObject();
Serializable obj;
while (iStream.available() > 0) {
obj = (Serializable) oInstream.readObject();
lists1.add(obj);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO: handle exception
}
}
public static
InputStream iStream = null;
ObjectInputStream oInstream = null;
try {
iStream = new FileInputStream(file);
oInstream = new ObjectInputStream(iStream);
// oInstream.readObject();
T obj = (T) oInstream.readObject();
return obj;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
oInstream.close();
oInstream.close();
}
return null;
}
public static
T obj) {
OutputStream oStream = null;
ObjectOutputStream oOutStream = null;
try {
oStream = new FileOutputStream(file, true);
if (file.length() > 1) {
oOutStream = new MyObjectOutputStream(oStream);
} else {
oOutStream = new ObjectOutputStream(oStream);
}
oOutStream.writeObject(obj);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
oStream.close();
oOutStream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
这个MyObjectOutputStream类是继承了ObjectOutputStream类,处理了写入时的异常:
public class MyObjectOutputStream extends ObjectOutputStream {
public MyObjectOutputStream(OutputStream arg0) throws IOException {
super(arg0);
}
@Override
protected void writeStreamHeader() throws IOException {
}
}