java为什么ObjectOutputStream将符号写入文件而不是我的输入?
我正在尝试将名为Clients
的ArrayList
写入文件。它可以工作,但不幸的是,它只是将符号写入文件,而不是我的实际输入
下面是我的代码:
public void SaveToFile() {
try
{
FileOutputStream fos = new FileOutputStream("CustomerLists.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(Clients);
oos.close();
} catch (Exception ex) {
ex.printStackTrace();
}
我如何让它工作
# 1 楼答案
因为它不是文本文件。它是根据Java序列化协议编写的序列化数据,因为您使用的是
ObjectOutputStream.writeObject()
这也意味着
CustomerLists.txt
不是这个文件的合适名称如果需要文本,请使用
PrintWriter
或BufferedWriter