我两个相同的代码,都使用缓冲流为什么一个复制的图片可以打开而另外一个复制的图片打不开呢?
可以打开复制图片的代码:
package review;
import java.io.*;
public class FileTest {
public static void main(String[] args) {
BufferedInputStream bis = null;
BufferedOutputStream bio = null;
try {
File file = new File("D:\\Desktop\\a.jpg");
File file2 = new File("D:\\Desktop\\3.jpg");
FileInputStream fr = new FileInputStream(file);
FileOutputStream fw = new FileOutputStream(file2);
bis = new BufferedInputStream(fr);
bio = new BufferedOutputStream(fw);
byte[] ch = new byte[1024];
int len;
while ((len = bis.read(ch)) != -1) {
bio.write(ch, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null) {
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (bio != null) {
try {
bio.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
}
复制的图片打不开的代码:
package review;
import java.io.*;
public class BUfferedHomework {
public static void main(String[] args) {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
File file1 = new File("D:\\Desktop\\a.jpg");
File file2 = new File("D:\\Desktop\\b.jpg");
FileInputStream fis = new FileInputStream(file1);
FileOutputStream fos = new FileOutputStream(file2);
bis = new BufferedInputStream(fis);
bos = new BufferedOutputStream(fos);
byte[] buff = new byte[2048];
int len;
while ((len = bis.read()) != -1) {
bos.write(buff, 0, len);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (bis != null) {
bis.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if (bos != null) {
bos.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
这是程序运行的效果:
还请各位帮我看看这是什么情况呀?