抛出异常的处理方法
package file;
import java.io.*;
// P05 复制图片
public class TestCopyImg {
public static void main(String[] args) throws IOException {
long newTime = System.currentTimeMillis();
// 源文件目录
String srcImgPath = "/Users/hike/Desktop/java/JavaSE/1.png";
// 目标文件目录
String distImgPath = "/Users/hike/Desktop/java/JavaSE/aa/bb/cc/1.png";
InputStream fis = new FileInputStream(srcImgPath);
OutputStream fos = new FileOutputStream(distImgPath);
// 每读一个就去写一次,很麻烦,低效
// int read;
// while((read = fis.read()) != -1){
// fos.write(read);
// }
// 一般情况,我们定义这个bytes的时候,长度要定义为1024的整数倍
byte[] bytes = new byte[10];
int len;
StringBuilder sb = new StringBuilder();
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
fis.close();
fos.close();
System.out.println("一共经历了:" + (System.currentTimeMillis() - newTime) + " 毫秒");
}
}
用 一个字节一个字节的方式 复制图片,速度慢
如果我们用读取字节数组的方式复制图片,速度快,效率高,比较吃内存
捕获异常,异常代码的处理
package file;
import java.io.*;
// P05 复制图片 不抛出异常,捕获异常
public class TestCopyImg2 {
public static void main(String[] args) {
long newTime = System.currentTimeMillis();
// 源文件目录
String srcImgPath = "/Users/hike/Desktop/java/JavaSE/2.png";
// 目标文件目录
String distImgPath = "/Users/hike/Desktop/java/JavaSE/aa/bb/cc/1.png";
InputStream fis = null;
OutputStream fos = null;
// 一般情况,我们定义这个bytes的时候,长度要定义为1024的整数倍
byte[] bytes = new byte[10];
int len;
StringBuilder sb = new StringBuilder();
try {
fis = new FileInputStream(srcImgPath);
fos = new FileOutputStream(distImgPath);
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fis != null) {
fis.close();
}
if (fos != null) {
fos.close();
}
} catch (IOException e) {
e.printStackTrace();
}finally {
fis = null;
fos = null;
}
}
System.out.println("一共经历了:" + (System.currentTimeMillis() - newTime) + " 毫秒");
}
}
再次优化
package file;
import java.io.*;
// P05 复制图片 不抛出异常,捕获异常 优化代码 java7之后 引入了 try-with-resource语句,默认执行到之后,自动执行流的关闭命令
public class TestCopyImg3 {
// 测试
private static class MyInputStream extends FileInputStream{
public MyInputStream(String name) throws FileNotFoundException {
super(name);
}
public MyInputStream(File file) throws FileNotFoundException {
super(file);
}
public MyInputStream(FileDescriptor fdObj) {
super(fdObj);
}
@Override
public void close() throws IOException {
super.close();
System.out.println("MyInputStream 的关闭方法被调用");
}
}
private static class MyOutputStream extends FileOutputStream{
public MyOutputStream(String name) throws FileNotFoundException {
super(name);
}
public MyOutputStream(String name, boolean append) throws FileNotFoundException {
super(name, append);
}
public MyOutputStream(File file) throws FileNotFoundException {
super(file);
}
public MyOutputStream(File file, boolean append) throws FileNotFoundException {
super(file, append);
}
public MyOutputStream(FileDescriptor fdObj) {
super(fdObj);
}
@Override
public void close() throws IOException {
super.close();
System.out.println("MyOutputStream 的关闭方法被调用");
}
}
public static void main(String[] args) {
long newTime = System.currentTimeMillis();
// 源文件目录
String srcImgPath = "/Users/hike/Desktop/java/JavaSE/2.png";
// 目标文件目录
String distImgPath = "/Users/hike/Desktop/java/JavaSE/aa/bb/cc/2.png";
// 一般情况,我们定义这个bytes的时候,长度要定义为1024的整数倍
byte[] bytes = new byte[10];
int len;
try(InputStream fis = new MyInputStream(srcImgPath);OutputStream fos = new MyOutputStream(distImgPath);) {
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("一共经历了:" + (System.currentTimeMillis() - newTime) + " 毫秒");
}
}
简化下来其实就是这样
package file;
import java.io.*;
// P05 复制图片 不抛出异常,捕获异常 优化代码 java7之后 引入了 try-with-resource语句,默认执行到之后,自动执行流的关闭命令
public class TestCopyImg3 {
public static void main(String[] args) {
long newTime = System.currentTimeMillis();
// 源文件目录
String srcImgPath = "/Users/hike/Desktop/java/JavaSE/2.png";
// 目标文件目录
String distImgPath = "/Users/hike/Desktop/java/JavaSE/aa/bb/cc/2.png";
// 一般情况,我们定义这个bytes的时候,长度要定义为1024的整数倍
byte[] bytes = new byte[10];
int len;
try(InputStream fis = new FileInputStream(srcImgPath);OutputStream fos = new FileOutputStream(distImgPath);) {
while ((len = fis.read(bytes)) != -1) {
fos.write(bytes, 0, len);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("一共经历了:" + (System.currentTimeMillis() - newTime) + " 毫秒");
}
}