【问题标题】:FileOutputStream opens a new file and deletes the contents whenever it is createdFileOutputStream 打开一个新文件并在创建时删除其内容
【发布时间】:2013-03-14 00:13:27
【问题描述】:

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

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

获取一个linkedlist来检查这个list是否包含这个对象。但是,FileOutputStream 总是会创建一个没有内容的新文件。我知道我是否将“true”值作为第二个参数传递给附加文件,但这个 .txt 文件应该只包含一个链表对象。因此,我不想在每个进程中附加一个列表。我该如何处理问题?我只想要

  • 如果指定的文件没有内容就不要读取对象,只添加第一次列表
  • 如果有指定文件的内容(意味着只有一个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 作为第二个参数,它将附加链表。

【问题讨论】:

  • 删除了什么?评论还是什么?
  • 我有在这个文件中序列化的链表对象。
  • 如果要追加项目,请不要序列化列表对象。而是一个接一个地序列化项目。这样您就可以始终使用附加模式。
  • 好吧,我不想附加链表。我只想读取包含客户端对象的链表对象,经过一些检查操作后,我会将其再次写回文件中
  • 如果选择多个对象,链表总是失败,请先尝试反序列化它们。

标签: java file outputstream fileoutputstream


【解决方案1】:
  • 如果指定的文件没有内容就不要读取对象,只添加第一次列表
  • 如果有指定文件的内容(意味着只有一个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();
}

【讨论】:

    【解决方案2】:

    试试这个:

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

    FileOutputStream(String name,boolean append)

    创建一个文件输出流以写入指定的文件 姓名。如果第二个参数为真,那么字节将被写入 文件的结尾而不是开头。一个新的文件描述符 创建对象来表示此文件连接。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-09
      • 2020-12-10
      • 2015-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多