其实主要是判断有没有复制成功,不然就不能删除这个文件
public static void main(String[] args) {
FileInputStream fis = null;
FileOutputStream fos = null;
//1.读取图片
File infile = new File("f:\\1.jpg");
File outfile = new File("f:\\8.jpg");
boolean isflag=false;
try {
fis = new FileInputStream(infile);
//2.写图片
fos = new FileOutputStream(outfile);
byte[] b = new byte[1024];
int length = -1;
while ((length = fis.read(b)) != -1) {
fos.write(b, 0, length);
}
fos.flush();
isflag=true;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
if(isflag){
infile.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}