日期:2014-05-20 浏览次数:21043 次
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class CopyImageDemo { public static void main(String[] args) { FileInputStream fis = null; FileOutputStream fos = null; try { fis = new FileInputStream("c:/a.jpg"); fos = new FileOutputStream("c:/b.jpg"); try { System.out.println("文件大小:" + fis.available() + "Byte"); byte buf[] = new byte[fis.available()]; int len = 0; while ((len = fis.read(buf)) != -1) { fos.write(len); fos.flush(); } } catch (IOException e) { e.printStackTrace(); } } catch (FileNotFoundException e) { e.printStackTrace(); } finally { try { if (fis != null) { fis.close(); } if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
// 新建文件输入流并对它进行缓冲 inBuff = new BufferedInputStream(new FileInputStream(sourceFile)); // 新建文件输出流并对它进行缓冲 outBuff = new BufferedOutputStream(new FileOutputStream(targetFile)); // 缓冲数组 byte[] b = new byte[1024 * 5]; int len; while ((len = inBuff.read(b)) != -1) { outBuff.write(b, 0, len); } // 刷新此缓冲的输出流 outBuff.flush();
------解决方案--------------------
建议:
用BufferedInputStream包装FileInputStream
用BufferedOutputStream包装FileOutputStream
flush方法写在while循环的外面