文件复制

import java.io.*;

//IO操作的工具类
public class IOUtils {
/*
这个方法用来完成 文件 的复制
frompath 是指源文件的路径
topath 是指目标文件的路径
*/
public static void copyOf(String frompath,String topath) throws IOException {
//TODO 读取源文件,写出到目标文件里去
//1,创建读取流和写出流
InputStream in = new BufferedInputStream( new FileInputStream(frompath) );
OutputStream out = new BufferedOutputStream( new FileOutputStream(topath) );
//2,边读边写
int b = 0 ;//定义变量,记录读到的数据
while( ( b = in.read() ) != -1){//读到了-1就表示没数据啦,停吧
out.write(b);//写出去
}
//3,释放资源
in.close();
out.close();
}

}


import java.io.IOException;
//测试 IO的工具类
public class Test5 {
public static void main(String[] args) throws IOException {
String from = "D:\\iotest\\1.jpg";
String to = "D:\\iotest\\111.jpg";
IOUtils.copyOf(from,to);
}
}
posted @ 2020-10-22 23:22  Liang-shi  阅读(148)  评论(0)    收藏  举报