package tongxin0;
/*
复制一个图片
思路:
1,用字节读取流对象和图片关联。
2,用字节写入流对象创建一个图片文件。用于存储获取到的图片数据。
3,通过循环读写,完成数据的存储。
4,关闭资源。*/import java.io.*;
class  CopyPic
{
public static void main(String[] args) 
{
CopyCicture();
}
public static void CopyCicture()
{
 BufferedInputStream bufis = null;
  BufferedOutputStream bufos = null;
try
{
bufos =new BufferedOutputStream( new FileOutputStream("d:\\test\\qq3.jpeg"));
bufis = new BufferedInputStream( new FileInputStream("d:\\test\\qq.jpeg"));
byte[] buf = new byte[1024];
int len = 0;
while((len=bufis.read(buf))!=-1)
{
bufos.write(buf,0,len);
}
}
catch (IOException e)
{
throw new RuntimeException("复制文件失败");
}
finally
{
try
{
if(bufis!=null)
bufis.close();
}
catch (IOException e)
{
throw new RuntimeException("读取关闭失败");
}
try
{
if(bufos!=null)
bufos.close();
}
catch (IOException e)
{
throw new RuntimeException("写入关闭失败");
}
}
}
}

解决方案 »

  1.   

    byte[] buf = new byte[1024];
    如果是图片,是不是应该数组设大一点,就1K能放什么图片呢?
      

  2.   

    很简单 图片后缀你看看是不是.jpg,换.jpeg就行了
      

  3.   

    另外
    while((len=bufis.read(buf))!=-1)

    bufos.write(buf,0,len);
    //bufos.flush();
    }
    这里最好flush一下
      

  4.   

    继续补充一下 既然你用到了BufferedInputStream 用不着byte数组了吧 多余了BufferedInputStream自己会又一个内部缓冲区,你可以用这个构造函数用size指定缓冲区大小的
    BufferedInputStream(InputStream in, int size) 
      

  5.   

    楼主要搞清楚哪个是读那个是写bufos =new BufferedOutputStream( new FileOutputStream("d:\\test\\qq3.jpeg"));
    bufis = new BufferedInputStream( new FileInputStream("d:\\test\\qq.jpeg"));
    按这样的写法,意思应该是把qq.jpeg复制一份保存为qq3.jpeg。在复制前最好先看一下qq.jpeg是否能正常打开。另外,bufos直接close就可以,不用flush。底层已经帮你flush过了
      

  6.   

    FileInputStram里面实现了InputStream的read();这个read()定义为native的 ,具体与操作系统硬盘的复制api有关,实现一次硬盘io读一个字节。(这个java管不了),另外重写了read(byte[] b, int off, int len)方法,而这个重写的方法里面调用的是FileInputStream独有的 private native int readBytes(byte b[], int off, int len) 方法,这个方法也是本地的方法。与硬盘io的直接操作有关,应该用c写的,这个和那个read()有根本的区别,就是一次io读b个字节!BufferedInputStream里面继承了FilterInputStream里面的InputStream成员in,就在你构造这个BufferedInputStream对象的时候FileInputStram传进去了,BufferedInputStream的read()调用的自己的fill(),而fill()调用的FilterInputStream的read(byte b[], int off, int len),继而调用传进去的FileInputStram的read(byte b[], int off, int len),也就调用了native int readBytes(byte b[], int off, int len) 方法。 从而实现了缓存。
    你也可以看看BufferedInputStream里面的read(byte b[], int off, int len),方法,都是调用的一样的native方法,具体二者为什么会有时差,和缓存大小有关!就是和readByte里传的b的大小有关!仅此而已。什么方法怎么写都会回归到最原始和windows操作系统打交道的微软上,这个c++,我放弃的真不对。
      

  7.   

    import java.io.BufferedInputStream;
    import java.io.BufferedOutputStream;
    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    public class copyFile {
    public static void main(String[] args) throws Exception {
    File f = new File("D:\\aa.jpg");
    File f1 = new File("D:\\as.jpg");
    BufferedInputStream br = new BufferedInputStream(new FileInputStream(f)); BufferedOutputStream bo = new BufferedOutputStream(
    new FileOutputStream(f1)); byte[] buf = new byte[2048];
    int len = br.read(buf);//
    while (len != -1) {
    bo.write(buf, 0, len);
    len = br.read(buf);
    }
    bo.flush();
    br.close();
    bo.close();
    }
    }