Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Copy a File

Status
Not open for further replies.

YerMom

Programmer
Oct 3, 2006
127
US
I want to copy a file from one directory to another. I read thread269-317473, which explains how to use the renameTo method on the File class, but renameTo moves a file instead of copying it.

I looked at the API specs for the FILE class and saw the following constructor:

Code:
File( File parent, String child)

The documentation is not entirely clear on what the method does -- I was thinking it performs a copy operation, but I tested it and the file is not copied.
1) Can anyone provide insight on what the method does?
2) Outside of opening a file and copying a file byte by byte, is there a way copy a file?

Thanks.
 
You have to copy the file yourself.
Considering the noddy factor of the code to do it, I have no idea why Sun never implemented it. Still, there you go. Pretty much every programmer should have a "I wish java did this" programme, which should certainly consist of something like the following :


Code:
String src = "src";
String dest = "dest";

File f = new File(src);
FileInputStream fis = new FileInputStream(f);
byte[] data = new byte[(int)f.length()];
fis.read(data);
fis.close();

FileOutputStream fos = new FileOutputStream(dest);
fos.write(data);
fos.flush();
fos.close();

File f2 = new File(dest);
if (f.length() != f2.length()) {
 throw new IOException("Copy of " +src +" to " +dest +" failed (no specific error form java, but src and dest lengths differ");
}

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Thanks Dianect and sedj. I implemented a solution like sedj suggested.

But back to my original post -- do you know what the following constructor does?

Code:
File( File parent, String child)

Thanks.

 
From the docs :

" Creates a new File instance from a parent abstract pathname and a child pathname string."

I expect the single String constuctor is a convenience constructor that calls the above one.

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
I think the documentation is prettu clear:

Code:
public File(File parent,
            String child)Creates a new File instance from a parent abstract pathname and a child pathname string. 
If parent is null then the new File instance is created as if by invoking the single-argument File constructor on the given child pathname string. 

Otherwise the parent abstract pathname is taken to denote a directory, and the child pathname string is taken to denote either a directory or a file. If the child pathname string is absolute then it is converted into a relative pathname in a system-dependent way. If parent is the empty abstract pathname then the new File instance is created by converting child into an abstract pathname and resolving the result against a system-dependent default directory. Otherwise each pathname string is converted into an abstract pathname and the child abstract pathname is resolved against the parent.

Cheers,
Dian
 
Heh, I have the same thing Dian posted in my clip board. I figgure you've read this before posting since you said the doc was vauge. So, I'll break it down a little more:

Parent is the parent of the child. Meaning, if the File is:
/home/user/foo then parent can be "/home/user" and the child "foo" OR "/home" the parent and "user/foo" the child.

The usefulness comes in the fact that you may have a group of working files in one directory that is set. Then the only part you need to prompt for is the file.

The buffer isn't strictly needed in the exaple above, and you can easly create Buffered streams on either or both end because of the wrapper pattern used in streams, readers and writers of the API.

[plug=shameless]
[/plug]
 
jstreich,

Thanks that makes sense. I was going to google for the information but you beat me to it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top