简体   繁体   中英

in Java Programming, will the file (.txt, .ser etc) get overwritten when we rewrite on existing file?

if I Do like this:

FileOutputStream fos=new FileOutputStream("cse.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(Dictionary);

and Again if I write the same file using the another object of Same type Dictionary on the same file like

FileOutputStream fos=new FileOutputStream("cse.txt");
ObjectOutputStream oos=new ObjectOutputStream(fos);
oos.writeObject(List);

Will that file get overwritten ?

If you write simply:

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

Then the old data will be lost and will get overridden by the new data.

However, if you write:

FileOutputStream fos=new FileOutputStream("cse.txt", true);

Then the old data will NOT be lost and the new data will be appended to the old one.

Here, the second argument true indicates that the bytes will be appended to the end of the file rather than overwriting the complete file.

See the documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM