IO流拷贝demo
package pool;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class IoCopy {
public static void main(String[] args) {
//读和写(内存) 读:键盘- ->内存 写:内存- -> 文件(硬盘)
//字节(byte)/ 字符(2byte)
long s1=System.currentTimeMillis();//记录拷贝时间
try(InputStream in=new FileInputStream("C:\iotest");
OutputStream out=new FileOutputStream("D:\iotest");){
int n=0;
//缓存数据
byte[] buffer =new byte[1024];
while(n!=-1) { //n=-1时终止拷贝
n=in.read(buffer);//读入1024字节
out.write(buffer);
}
}catch(IOException e) {
}
//try...with...Rescource 捕获异常的方式之一。这种方式的好处在于会自动关闭资源(IO对象),不用写finally,
//使用这个方法的前提是try的内容底层实现了closeable接口
long s2=System.currentTimeMillis();
System.out.println("执行时间:"+(s2-s1));
}
}
iotest文件从D盘拷贝到c盘了



