java IO(二)大文件复制

时间:2024-01-18 19:11:44
package cn.sasa.demo3;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException; public class CopyDemo {
public static void main(String[] args) {
long start = System.currentTimeMillis();
/**
* 复制文件
*/
FileInputStream input = null;
FileOutputStream output = null;
try {
input = new FileInputStream("D:\\sasa\\Java\\WebApiCode.zip");
output = new FileOutputStream("D:\\sasa\\abc.zip");
byte[] buffer = new byte[1024 * 1024];
int len = 0;
while((len = input.read(buffer)) != -1) {
output.write(buffer);
}
}catch(IOException e) {
System.out.println(e.getMessage());
System.out.println("复制失败");
}finally {
try {
if(input != null)
input.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println("资源释放失败");
}
try {
if(output != null)
output.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(e.getMessage());
System.out.println("资源释放失败");
}
}
long end = System.currentTimeMillis();
System.out.println(end - start);
}
}