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?
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
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.