FileOutputStream打开一个新文件,并在创建内容时删除它们 [英] FileOutputStream opens a new file and deletes the contents whenever it is created

查看:185
本文介绍了FileOutputStream打开一个新文件,并在创建内容时删除它们的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm dealing with serialization and deserialization in files. Moreover, i'm stack about using FileOutputStream with ObjectOutputStream. The issue is i have server/client chat application and whenever a client is connected, server must check that if connected client has registered before. So, when a client is connected to server, server creates an output stream like

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

获取链表以检查此列表是否包含此对象。但是, FileOutputStream 始终创建一个没有内容的新文件。我知道如果我传递一个true值作为附加文件的第二个参数,但是这个.txt文件应该只包含一个作为链表的对象。因此,我不想在每个过程中附加一个列表。我怎么处理问题?我只想要

to get a linkedlist to check whether this list contains this object or not. But, FileOutputStream creates always a new file with no contents. I know if i pass a "true" value as a second parameter to append file but this .txt file should contain only one object which is a linkedlist. Therefore, i dont want to append a list in every process. How can i handle with issue ? I just only want


  • 如果有没有内容的speficied文件没有读取对象,只需要第一次添加列表

  • 如果指定的文件包含内容(意味着只有一个linkedlist对象),则读取它,然后将新客户端添加到列表中,并将linkedlist对象写回文件。 / li>
  • If there is speficied file with no contents don't read object, just add the list for only first time
  • If there is the specified file with contents (mean that there is only one linkedlist object) then read it, and add the new client into the list and write the linkedlist object back into the file.

我希望我对你的问题很清楚,我会对每一个答案表示赞赏。所以非常感谢

I hope that i had been clear to you about my problem and i will appreciated for every answer. So thanks anyway

编辑:这是我想说的一个简单的例子。

here is a simple example of what i'm trying to say.

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 ,它将附加链接列表。

Every time i run this code FileOutputStream opens a new file. However, if i use true for second parameter it will append the linkedlists.

推荐答案



  • 如果有没有内容的特定文件不读取对象,只需首次添加列表

  • 如果指定的文件包含内容(意味着只有一个linkedlist对象),则读取它,并将新客户端添加到
    列表中,并将linkedlist对象写回文件。



您可以通过以下方式执行此操作:


You can do this in following way:

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();
}

这篇关于FileOutputStream打开一个新文件,并在创建内容时删除它们的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆