wood burning stoves*
The moose likes I/O and Streams and the fly likes copying a file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "copying a file" Watch "copying a file" New topic
Author

copying a file

Smitha Vallaru
Greenhorn

Joined: Aug 15, 2006
Posts: 16
I am trying to copy an xml file of 13KB to new file using File readers, BufferReader and FilerWriter. But its only copying 8kb. There is no logic just read each line from one file and copy it newly created file.

Can anybody let me know, is there size limitation for copying file while using File Readers. I have even tried giving size of FileReader and writer.

Thanks
Smitha.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18708
    
    8

No, of course there isn't a limitation on the size of the files you can copy.

When you look at the output file, is it the same as the input file only with some data at the end missing? If so, then most likely you aren't closing your output writer.

But you would be better off copying the file using InputStreams and OutputStreams. If you don't plan to do anything with the data except copy it, there's no point in converting the bytes to chars and then back to bytes. And if you don't use the right charset for your Reader, you could even corrupt the data.
Smitha Vallaru
Greenhorn

Joined: Aug 15, 2006
Posts: 16
Wow!that was pretty fast. you are right i was not closing the file. Actually i wast testing code and once i finalize I thought of closing and setting it null. But, I never thought it would cost me a day.

Thanks
Smitha.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
It also seems quite possible that you're not copying the file correctly. One common error would be failing to check the return value of a read(char[]) call to verify how many chars were really read. If you show the code you used to copyy the file, we can help identify possible problems.


"I'm not back." - Bill Harding, Twister
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42360
    
  64
There are also certain obscure circumstances where files can't be copied with streams/readers/writers, e.g. Macintosh OS 9 files having resource forks. Trying to copy those with the java.io classes will result in truncated files.


Ping & DNS - my free Android networking tools app
Kevin Ott
Greenhorn

Joined: Dec 04, 2006
Posts: 3
It could be that you're not copying the file correctly. Here is some example source code showing how you can copy a file:



This listing is posted at e1071. Copying One File to Another

There are many more java source code examples at
http://exampledepot.com
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: copying a file