File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes copying contents of one file to another Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "copying contents of one file to another" Watch "copying contents of one file to another" New topic
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: 37926
    
  22
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: 19649
    
  18

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37926
    
  22
Thanks, Rob
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: copying contents of one file to another
 
Similar Threads
Copying data into a file using inputstream
files are not beened deleted and renamed
Java And OpenDocument Files
displaying an image of type TIF
using byte array to copy contents of one file to another