package demo3;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Demo3 {
// 文件的拷贝
public static void main(String[] args) {
FileInputStream fi = null;
FileOutputStream fo = null;
try {
// 1 准备输入输出管道,指向相应的位置
fi = new FileInputStream(new File("D:\"));
fo = new FileOutputStream(new File("c"));
// 2 准备缓冲区
byte[] buffer = new byte[1024];
int len = 0;
// 3 循环拷贝文件
long startTime = System.currentTimeMillis();
while ((len = fi.read(buffer)) != -1) {
fo.write(buffer, 0, len);
}
long endTime = System.currentTimeMillis();
System.out.println("运行时间:" + (endTime - startTime));
} catch (Exception e) {
e.printStackTrace();
} finally {
// 4 拷贝完成 , 关闭资源
try {
fi.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
package demo4;
import java.awt.;
import java.io.;
public class Demo4 {
// 文件的拷贝
public static void main(String[] args) {
BufferedInputStream fi = null;
BufferedOutputStream fo = null;
try{
// 1 准备输入输出管道,指向对应的位置
fi = new BufferedInputStream( new FileInputStream("D:\") );
fo = new BufferedOutputStream( new FileOutputStream("c:\") );
// 2 准备缓冲区
byte[] buffer = new byte[1024];
int len = 0;
// 3 循环拷贝文件
long startTime = System.currentTimeMillis();
while ( ( len = fi.read(buffer)) != -1 ){
fo.write(buffer , 0 , len);
}
long endTime = System.currentTimeMillis();
System.out.println("运行时间:" + (endTime-startTime));
}catch (Exception e ){
e.printStackTrace();
}finally {
// 4 拷贝完成 , 关闭资源
try {
fi.close();
}catch (IOException e ){
e.printStackTrace();
}
try {
fo.close();
}catch (IOException e ){
e.printStackTrace();
}
}
}
}