public class FileDemo {
public static void main(String[] args) {
try {
File f = new File("e:/e/123456.jpg");
FileInputStream ff = new FileInputStream(f);
BufferedInputStream bf = new BufferedInputStream(ff);
FileOutputStream fo = new FileOutputStream(f.getAbsolutePath());
BufferedOutputStream bo = new BufferedOutputStream(fo);
byte[]b=new byte[1024];
int len=0;
while((len=bf.read(b))!=-1) {
bo.write(b);
bo.flush();
}
bo.close();
fo.close();
bf.close();
ff.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我用这个程序读来一个图片,然后发现
就是这样的。请问我的程序哪里有问题?谢谢解答
我把你的代码调试了一下发现,,这句代码有很大问题
FileOutputStream fo = new FileOutputStream(f.getAbsolutePath());
输出流会把文件清空,,,也就是你这句话会把 你的 f文件清空,,,可以找一个文本文件测试一下下面代码:
File f = new File("G:/test.txt");
FileInputStream ff = new FileInputStream(f);
BufferedInputStream bf = new BufferedInputStream(ff);
File f2 = new File("G:/test.txt");
FileOutputStream fo = new FileOutputStream(f2);
通过源码发现,,确实是要清空的,,源码中有个是否追加的参数,,默认是falst(不追加,清空)
你需要修改路径,,如下:
public static void main(String[] args) {
try {
File f = new File("e:/e/123456.jpg");
FileInputStream ff = new FileInputStream(f);
BufferedInputStream bf = new BufferedInputStream(ff);
FileOutputStream fo = new FileOutputStream("e:/e/10086.jpg");
BufferedOutputStream bo = new BufferedOutputStream(fo);
byte[]b=new byte[1024];
int len=0;
while((len=bf.read(b))!=-1) {
bo.write(b);
bo.flush();
}
bo.close();
fo.close();
bf.close();
ff.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
如上代码,,完美执行,,,有问题还可以追问,,,木有问题,请采纳