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 EJB 3 in Action this week in the EJB and other Java EE Technologies 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: 36453
    
  15
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: 19538
    
  16

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: 36453
    
  15
Thanks, Rob
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: copying contents of one file to another
 
Similar Threads
Java And OpenDocument Files
Copying data into a file using inputstream
displaying an image of type TIF
files are not beened deleted and renamed
using byte array to copy contents of one file to another