• Post Reply Bookmark Topic Watch Topic
  • New Topic

Perplexed about unusual behavior when copying files using FileInputStream and FileOutputStream  RSS feed

 
Benedict Wong
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code snippet, which is supposed to copy contents from the first file to the second file, something unusual occurred when I made a mistake.

On Line 6, when I accidentally changed args[1] to args[0], the contents of the first file totally disappeared. I expected fin and fout to be copying contents from the first file back to the first file, therefore contents of both files would have remained unchanged. So why did the contents of the first file disappear instead?
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why did the contents of the first file disappear instead?

I think the constructor for the FileOutputStream class creates a new file to write to.  If the file exists it wipes it out.

For an experiment, add the append parameter to the constructor and see what happens.
 
Benedict Wong
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the explanation, Norm!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!