![]() |
The Java Developers Almanac 1.4 |
|
e1071. Copying One File to AnotherThis example uses file streams to copy the contents of one file to another file. See e172 Copying One File to Another for an example that uses file channels. // Copies src file to dst file.
// If the dst file does not exist, it is created
void copy(File src, File dst) throws IOException {
InputStream in = new FileInputStream(src);
OutputStream out = new FileOutputStream(dst);
// Transfer bytes from in to out
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
}
e20. Creating a File e21. Getting the Size of a File e22. Deleting a File e23. Creating a Temporary File e24. Renaming a File or Directory e25. Moving a File or Directory to Another Directory e26. Getting and Setting the Modification Time of a File or Directory e27. Forcing Updates to a File to the Disk
© 2002 Addison-Wesley. |