有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么ObjectOutputStream将符号写入文件而不是我的输入?

我正在尝试将名为ClientsArrayList写入文件。它可以工作,但不幸的是,它只是将符号写入文件,而不是我的实际输入

下面是我的代码:

    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) 个答案

  1. # 1 楼答案

    Why is it when I run this java program it creates the text file but it prints out symbols instead of my input?

    因为它不是文本文件。它是根据Java序列化协议编写的序列化数据,因为您使用的是ObjectOutputStream.writeObject()

    这也意味着CustomerLists.txt不是这个文件的合适名称

    如果需要文本,请使用PrintWriterBufferedWriter