ObjectOutputStream oos = new ObjectOutputStream( new FileOutputStream(file) );
时间: 2024-06-04 17:10:16 浏览: 25
这行代码是用于创建一个将 Java 对象序列化并写入文件的 ObjectOutputStream 对象。其中,FileOutputStream 的作用是将数据写入文件,而 ObjectOutputStream 则是将 Java 对象序列化为字节流写入 FileOutputStream 中。这样,我们就可以通过反序列化来重新读取 Java 对象,从而达到数据持久化的目的。需要注意的是,写入对象的类必须实现 Serializable 接口,否则会抛出 NotSerializableException 异常。
相关问题
优化这段代码public class DataIO { public static void savePersonList(List<Person> list, String fileName) throws IOException { ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName)); oos.writeObject(list); oos.close(); } public static List<Person> loadPersonList(String fileName) throws IOException, ClassNotFoundException { ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName)); List<Person> list = (List<Person>) ois.readObject(); ois.close(); return list; } }使其能够输出保存成功,保存失败,读入成功,读入失败的System
可以在方法中添加try-catch语句,捕获异常并输出对应的信息。修改后的代码如下:
```
public class DataIO {
public static void savePersonList(List<Person> list, String fileName) {
try {
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(fileName));
oos.writeObject(list);
oos.close();
System.out.println("保存成功!");
} catch (IOException e) {
System.out.println("保存失败!");
e.printStackTrace();
}
}
public static List<Person> loadPersonList(String fileName) {
try {
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(fileName));
List<Person> list = (List<Person>) ois.readObject();
ois.close();
System.out.println("读入成功!");
return list;
} catch (IOException | ClassNotFoundException e) {
System.out.println("读入失败!");
e.printStackTrace();
return new ArrayList<>();
}
}
}
```
这样,在保存和读取文件时,若出现异常,就会输出对应的信息。如果保存成功或读取成功,则也会输出对应的信息。
private void saveList(List<Users> list) { //声明对象流对象 ObjectOutputStream oos = null; try { //创建对象流对象 oos = new ObjectOutputStream(new FileOutputStream("user.bat")); //写对象 oos.writeObject(list); } catch (Exception e) { e.printStackTrace(); } finally { try { oos.close(); } catch (Exception e) { e.printStackTrace(); } } }
这段代码是一个保存用户列表的方法。它使用了对象流来将用户列表写入到文件中。首先,它创建了一个 ObjectOutputStream 对象,将其初始化为将数据写入到名为 "user.bat" 的文件中。然后,它使用 writeObject() 方法将用户列表写入到文件中。如果在这个过程中发生了任何异常,它会打印出异常的堆栈跟踪信息。最后,它在 finally 块中关闭了对象输出流。这样可以确保在方法执行完毕后,资源得到正确释放。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)