话不多说 直接上代码
//main方法
public static void main(String[] args) throws IOException {
String srcpath = "day11_data/img/cropped-1920-1080-1012576.jpg";
String dppath = "day11_data/d/bbb.jpg";
copyFile(srcpath, dppath);
//copyFileByArray(srcpath,dppath);
}
//copyFile方法
public static void copyFile(String srcPath, String dpPath) throws IOException {
//准备流
FileOutputStream fos = new FileOutputStream(srcPath);
FileInputStream fis = new FileInputStream(dpPath);
int b;
while ((b = fis.read()) != -1) {
fos.write(b);
//System.out.println(b);
}
fos.close();
fis.close();
}
public static void copyFileByArray(String srcPath, String destPath) throws IOException {
//准备流
FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(destPath);
long l = System.currentTimeMillis();
//输入
int len;
byte[] bytes = new byte[1024];
while ((len = fis.read(bytes)) != -1) {
//输出
fos.write(bytes, 0, len);
}
//关闭流
fos.close();
fis.close();
long l2 = System.currentTimeMillis();
System.out.println("复制图片用了" + (l2 - l) + "毫秒");
}
//报错
Exception in thread "main" java.io.FileNotFoundException: day11_data\d\bbb.jpg (系统找不到指定的文件。)
原因是输入流与输出流的路径写反了
FileOutputStream fos = new FileOutputStream(srcPath);
FileInputStream fis = new FileInputStream(dpPath);
这里,输入流应该是srcPath,输出流是dpPath,调换一下就可以解决这个问题了.