FileOutputStream打开一个新文件并在创建时删除其内容。

3

我正在处理文件中的序列化和反序列化。此外,我在使用FileOutputStreamObjectOutputStream时遇到了问题。问题是,我有一个客户端/服务器聊天应用程序,每当连接一个客户端时,服务器必须检查已注册的客户端。因此,当客户端连接到服务器时,服务器会创建一个输出流,如下:

FileOutputStream fos = new FileOutputStream("src\\example\\mainList.txt");

为了检查这个列表是否包含这个对象,需要获取一个LinkedList。但是,FileOutputStream总是创建一个没有内容的新文件。我知道如果将"true"值作为第二个参数传递给追加文件,但这个.txt文件应该只包含一个LinkedList对象。因此,我不想在每个过程中都追加一个列表。我该如何解决这个问题?我只想要:
  • 如果指定的文件没有内容,则不读取对象,只添加列表一次
  • 如果存在指定的文件(表示只有一个LinkedList对象),则读取它,并将新客户端添加到列表中,并将LinkedList对象写回文件。
我希望我已经清楚地向您阐述了我的问题,我将感激每一个答案。所以无论如何感谢。
编辑:这里是我试图说的简单示例。
public class PersonTest2 {

    public static void main(String[] args) throws IOException, ClassNotFoundException {

        LinkedList<Person> list = new LinkedList<Person>();

        Person per = new Person("John","Thompson",22);
        Person per2 = new Person("Suzie","Cash",23);
        Person per3 = new Person("Peter","Jackson",24);
        Person per4 = new Person("Michael","Coch",25);

        list.add(per);
        list.add(per2);
        list.add(per3);
        list.add(per4);

        FileOutputStream fos = new FileOutputStream("person.txt");

        BufferedReader br = new BufferedReader(new FileReader("person.txt"));     
        if (br.readLine() == null ) {
            System.out.println("No errors, and file is empty");

        }

        ObjectOutputStream oos = new ObjectOutputStream(fos);

        oos.writeObject(list);
        fos.close();

        FileInputStream fis = new FileInputStream("person.txt");
        ObjectInputStream ois = new ObjectInputStream(fis);

        LinkedList<Person> list2;

        list2 = (LinkedList) ois.readObject();

        for (Iterator<Person> itr = list2.iterator(); itr.hasNext();) {
            Person rper = itr.next();

                System.out.println(rper.name + " " + rper.last_name + " " + rper.age);
        }
    }

}

每次运行此代码,FileOutputStream 都会打开一个新文件。但是,如果我对第二个参数使用 true,它将追加 LinkedLists。

它被删除了什么?评论还是其他东西? - user2173738
我有一个LinkedList对象,它被序列化在这个文件中。 - quartaela
如果您想要添加项目,请不要序列化列表对象。相反,一个接一个地序列化项目。这样您就可以始终使用追加模式。 - Eyal Schneider
我不想追加链表。我只想读取包含客户端对象的链表对象,并经过一些检查操作后再将其写回文件。 - quartaela
链表在选择多个对象时总是会失败,先尝试对它们进行反序列化。 - user2173738
显示剩余2条评论
2个回答

5
如果指定的文件没有内容,则不读取对象,仅在第一次添加列表。 如果指定的文件有内容(意味着只有一个LinkedList对象),则读取它,并将新客户端添加到列表中,然后将LinkedList对象写回文件。 可以按照以下方式完成此操作:
File file = new File("person.txt");
boolean toWrite = false;
boolean toModify = false;
if (file.exists())
{
   if (file.length() == 0)
   {
     toWrite = true;
   }
   else 
   {
     toModify = true;
   }
}
if (toWrite || !file.exists())
{
     FileOutputStream fos = new FileOutputStream(file);
     ObjectOutputStream oos = new ObjectOutputStream(fos);
     oos.writeObject(list);
     fos.close();
     oos.close();
}
else if (toModify)
{
   FileInputStream fins = new FileInputStream(file);
   ObjectInputStream oins = new ObjectInputStream(fins);
   LinkedList<Person> temp = (LinkedList<Person>)oins.readObject();
   fins.close();
   oins.close();
   temp.add(per);
   temp.add(per2);
   temp.add(per3);
   temp.add(per4);
   FileOutputStream fos = new FileOutputStream(file);
   ObjectOutputStream oos = new ObjectOutputStream(fos);
   oos.writeObject(temp);
   fos.close();
   oos.close();
}

4
尝试这个:

FileOutputStream fos = new FileOutputStream("src\\example\\mainList.txt" ,true);

FileOutputStream(String name, boolean append)

创建一个文件输出流,用于写入指定名称的文件。如果第二个参数为true,则字节将写入文件末尾而不是开头。创建一个新的FileDescriptor对象来表示此文件连接。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接