Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

copying contents of one file to another

 
linus dale
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

this program takes name of a file as command line argument
and copies contents of that file to a newly created file called newfile.txt

newfile.txt is formed but nothing is written to it

and if newfile.txt already exists,and has something in it,it becomes empty
why is this happening?





please help
thanks
 
Campbell Ritchie
Sheriff
Pie
Posts: 49788
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go through the Java™ tutorials section about I/O and have a look at the documentation in the API for FileOutputStream. I thought a FileOutoutStream was not suitable for .txt files, and you should use a FileWriter instead. But I am not up to speed about byte[] arrays, I am afraid; somebody else may have a better idea.
 
linus dale
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Please go through the Java™ tutorials section about I/O and have a look at the documentation in the API for FileOutputStream. I thought a FileOutoutStream was not suitable for .txt files, and you should use a FileWriter instead. But I am not up to speed about byte[] arrays, I am afraid; somebody else may have a better idea.



I got it
it was because I had 2 files called File3.java in different folders

i was making changes in the wrong file
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:I thought a FileOutoutStream was not suitable for .txt files, and you should use a FileWriter instead.

If you want to treat the contents as characters inside your Java code, then you're right. But for copying, FileInputStream is just fine. After all, the contents of any file is still in bytes, even if it does represent text. The bytes are simply copied, so the destination will still be the same as the source.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49788
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Rob
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic