| Author |
copying contents of one file to another
|
linus dale
Ranch Hand
Joined: Jul 01, 2009
Posts: 44
|
|
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
Joined: Oct 13, 2005
Posts: 32620
|
|
|
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
Joined: Jul 01, 2009
Posts: 44
|
|
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
Joined: Oct 27, 2005
Posts: 19216
|
|
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.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32620
|
|
Thanks, Rob
|
 |
 |
|
|
subject: copying contents of one file to another
|
|
|