<MARQUEE>第一次发帖  才疏学浅  请见谅</MARQUEE> 
这两天研究了下java  的 IO  就写了个读取图片的   请教还可以怎么写啊?最好注释下 。
好了 闲话不多说    下边是代码    public class Io读取图片 { public static void main(String[] args) { try {
BufferedInputStream fis = new BufferedInputStream(
new FileInputStream("D:\\Images\\knkan\\1.jpg")); BufferedOutputStream fos = new BufferedOutputStream(
new FileOutputStream("D:\\meinv.jpg"));
byte[] buf = new byte[1024];
int i = 0;
while ((i = fis.read(buf)) != -1) {
fos.write(buf, 0, i);
}
fis.close();
fos.close();
System.out.println("图片写入成功了");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}

解决方案 »

  1.   

    只要用二进制流就可以了。随便写了个 public static void main(String[] args){
    ImageInputStream iis = null;
    ImageOutputStream ios = null;
    try {
    iis = new FileImageInputStream(new File("C:\\Documents and Settings\\pKF58059\\My Documents\\My Pictures\\","4298534_1326781519AYll.png"));
    ios = new FileImageOutputStream(new File("C:\\Documents and Settings\\pKF58059\\My Documents\\My Pictures\\","a.png"));
    byte[] buf = new byte[1024 * 2];
    int i = 0;
    while((i=iis.read(buf)) != -1){
    ios.write(buf, 0, i);
    }
    ios.flush();
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    } finally{
    try {
    iis.close();
    ios.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
      

  2.   

    其实这个不用转成buffered流就行了。
    public static void main(String[] args) throws Exception {
    try {
    InputStream fis = new FileInputStream("D:\\Images\\knkan\\1.jpg"); OutputStream fos = new FileOutputStream("D:\\meinv.jpg");
    byte[] buf = new byte[4096];
    int i = 0;
    while ((i = fis.read(buf)) != -1) {
    fos.write(buf, 0, i);
    }
    fis.close();
    fos.close();
    System.out.println("图片写入成功了");
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
      

  3.   

    一般是根据什么来判断他的大小 byte[] buf = new byte[1024 * 2];
      

  4.   

    感谢大神们的回帖    首先分析下 我要操作的是图片  数据源是文件   也就是二进制流   
    选择底层FileinputStream  FileoutputStream流操作   还可以选择处理图片的流
           ImageInputStream  ?  
    那么如果是操作  java对象的序列化 反序列化的时候  用ObjectinputStream  那什么时候用到 bufferinputStream  ?
    请大家指点还有就是3楼朋友提到的  byte[] buf = new byte[1024 * 2];  缓冲大小是根据什么设置的那?