FileOutputStream opens a new file and deletes the

2019-02-25 04:01发布

问题:

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");

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

  • 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

EDIT: 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);
        }
    }

}

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

回答1:

  • 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.


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


回答2:

Try this:

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

FileOutputStream(String name,boolean append)

Creates a file output stream to write to the file with the specified name. If the second argument is true, then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection.