-1

What I want to achieve: When I save an Integer object to a file using the writeObject() method from ObjectOutputstream class, I want to overwrite the old Integer object and replace it with the new one. But I don't want to close and open again the stream every time I want to put a new Integer object. I just want to update it with new values.

The solution that I come up with didn't work for me. Here is the code:

ObjectOutputStream stream1 = new ObjectOutputStream(new FileOutputStream(new File("ClientBase"), false));

stream1.writeObject(new Integer(2));

stream1.flush();
stream1.reset();

stream1.writeObject(new Integer(9));
stream1.close();

When I read this, I have two Integer objects instead of an Integer with value 9 replaced by Integer with value 2.

If I put it like this, it works.

ObjectOutputStream stream1 = new ObjectOutputStream(new FileOutputStream(new File("ClientBase"), false));

stream1.writeObject(new Integer(2));
stream1.close();    

stream1 = new ObjectOutputStream(new FileOutputStream(newFile("ClientBase"), false));

stream1.writeObject(new Integer(9));
stream1.close();

My question: Am I using the reset() method in a wrong way and is there any other way to achieve overwriting without closing/opening stream?

René Scheibe
  • 1,970
  • 2
  • 14
  • 20
Le No
  • 3
  • 1
  • 2
  • It's a stream, not a random access thing. You write two objects to the stream, you will read two objects from it. Your question doesn't make sense. – user207421 Jan 30 '17 at 22:47
  • So the only way is to close and open stream again? – Le No Jan 30 '17 at 23:25
  • Not only the stream but the *file.* NB DId you consider consulting the Javadoc before you posted? Nothing there that supports your assumption in any way. – user207421 Jan 30 '17 at 23:46
  • Currently i am studying. I am using "Thinking in Java" book. In my head was perfectly possible,but i guess i dont understand streams. Anyway thank you for the answer. – Le No Jan 31 '17 at 00:10

1 Answers1

-1

This is what I tried.

However, note that I'm not sure this code is safe...:

public static void main(String[] args) {
    try (FileOutputStream fos = new FileOutputStream("objects.file");
            ObjectOutputStream os = new ObjectOutputStream(fos)) {
        long position = fos.getChannel().position();
        os.writeObject(new Integer(2));
        os.flush();
        os.reset(); // added line
        fos.getChannel().position(position);
        os.writeObject(new Integer(9));
        os.flush();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
SlumpA
  • 882
  • 12
  • 24
  • This is what you *coded*. You didn't try it. It won't work. The resulting file will throw a `StreamCorruptedException` when provided to `ObjectInputStream`. – user207421 Jan 30 '17 at 23:16
  • In my opinion, you shouldn't use this. I think it's safer to close and use a new ObjectOutputStream. However, even in that case, you might corrupt the rest of the file. – SlumpA Jan 31 '17 at 00:20