Java怎么实现文件拷贝
用FileInputStream、DataInputStream和FileOutputStream、DataOutputStream完成一个文件拷贝:将e:\test.jpg 拷贝到 e:\demo\test.jpg
用FileInputStream、DataInputStream和FileOutputStream、DataOutputStream完成一个文件拷贝:将e:\test.jpg 拷贝到 e:\demo\test.jpg
答案:建议看一下apache FileUtils.copyFile的源码
public class CopyFile {
public static void doCopyFile() throws IOException{
File srcFile = new File("e:\\test.jpg");
File destFile = new File("e:\\demo\\test.jpg");
FileInputStream input = new FileInputStream(srcFile);
try {
FileOutputStream output = new FileOutputStream(destFile);
try {
byte[] buffer = new byte[4096];
int n = 0;
while (-1 != (n = input.read(buffer))) {
output.write(buffer, 0, n);
}
} finally {
try {
if (output != null) {
output.close();
}
} catch (IOException ioe) {
// ignore
}
}
} finally {
try {
if (input != null) {
input.close();
}
} catch (IOException ioe) {
// ignore
}
}
}
}
上一个:请问各位java高手:
下一个:java异常编程题