aspose file tools*
The moose likes I/O and Streams and the fly likes why does this file copy not work? 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 » I/O and Streams
Bookmark "why does this file copy not work?" Watch "why does this file copy not work?" New topic
Author

why does this file copy not work?

mark baum
Greenhorn

Joined: Aug 21, 2001
Posts: 18
I tried to copy a file following the example in the java and nutshell tutorials as listed below. I wrote a junit test that creates a file with one line of text. The file is there, it is found and opened and the text is there too until the call of file.read(buffer). That method returns -1, after the call the source file is empty and nothing is written to the targetFile.

Could that be connected to the 8 and 16bit problem mentioned in the java tutorial?

"Remember that FileReader and FileWriter read and write 16-bit characters. However, most native file systems are based on 8-bit bytes. These streams encode the characters as they operate according to the default character-encoding scheme. You can find out the default character-encoding by using System.getProperty("file.encoding"). To specify an encoding other than the default, you should construct an OutputStreamWriter on a FileOutputStream and specify the encoding."

Does anybody has a fileCopy function that copies any sort of file?

Thanks for help!

[ March 07, 2006: Message edited by: Jim Yingst ]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24166
    
  30

Are you sure that in your test, targetDrive and/or targetDir are different from sourceFile's drive and/or dir? Because the synptoms you describe are pretty much what you'd see if you were writing to the same file you're reading: opening it for output would truncate it to zero length.


[Jess in Action][AskingGoodQuestions]
mark baum
Greenhorn

Joined: Aug 21, 2001
Posts: 18
Hi Ernest,

Thank you for the hint! It was true: the source and target file was on the same drive AND dir, but the name was different. But I'm a bit puzzeled, because I don't recall having read I must not use the same drive nor directory. So I cannot save a file in the same directory under another name with that method? Would there be an alternative in java to do this? (I searched on the net for doc but could not find any? can you please give me a hint for documentation on this?)

I also wonder if someone with more IO expertise could tell me, if this copy method would be robust for any type of file (binary ansi8 or 16 etc)

Thanks
mark baum
Greenhorn

Joined: Aug 21, 2001
Posts: 18
Sorry, I saw the wrong thought, if the target drive and dir are the same, the method writes to the same file...
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
"baum", your recent name change is incompatible with our display name policy. Please give it a read and change your display name to include a first and last name. Thanks.

I added [code] tags to your first post above to try to fix the indentation for readability. Unfortunately it looks like your indentation was inconsistent. Mixing tabs and spaces is typically a bad idea for this reason - it looks funky when displayed in other formats.

I can't tell from your last comment if this means EFH's suggestion solved your problem, or not.

As for the question about whether it's robust for all file types - looks like it should be, yes. The comment from the tutorial was about Readers and Writers, not InputStreams and OutputStreams. If you were tryign to treat a binary file as text by using a Reader, that could cause problems. But InputStream and OutputStream just deal with bytes, regardless of what those bytes mean. You shouldn't have a problem here. Assuming you can write a new file at all - failure may occur for other reasons. Point is, the use of the InputStream and OutputStream looks good once they've been created.


"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: why does this file copy not work?
 
Similar Threads
struts, where files are uploaded ??
Java File Error - Format Issue
How to move a file ? (and a few more java IO ques)
Copying BIG file
Displaying file content in a jsp page