最近遇到困难   我想把一个图片转化成二进制存入到记事本中    代码是这样的import java.io.*;
public class Upload {
   
    public static void main(String args[]) {
    int b,c;
    byte tom[]=new byte[1000];
    try{
        File f=new File("d:\\blue.jpg");
        InputStream in = new FileInputStream(f);
        FileOutputStream out = new FileOutputStream("d:\\line.txt");
        while((b=in.read(tom,0,100))!=-1){
          in.read(tom,0,1000);
          out.write(tom);
        }
        in.close();
        out.close();
    }catch(IOException e){
        System.out.print("file read err"+e);
    }
    }
}
  我又写了一段 读取 该文件的代码,我想实现从记事本里面 将它二进制流读出来 然后再显示出来
代码是这样的import java.io.*;
    public class show {
       
        public static void main(String args[]) {
        int b,c;
        byte tom[]=new byte[1000];
        try{
            File f=new File("d:\\line.txt");
            FileInputStream in = new FileInputStream(f);
            FileOutputStream out = new FileOutputStream("d:\\line.jpg");
            while((b=in.read(tom,0,100))!=-1){
              in.read(tom,0,1000);
              out.write(tom);
            }
            in.close();
            out.close();
        }catch(IOException e){
            System.out.print("file read err"+e);
        }
        }
    }
  当我 运行完这两段代码后 
就去打开line.jpg 
  但是 什么都没有  这是怎么回事呢??
 希望大家能帮助我一下
我是java新手 
  希望能给点注释 谢谢!!!

解决方案 »

  1.   

    在读取的时候不要采用多次读取
    while((b=in.read(tom))!=-1)
    {
        out.write(tom, 0, b);
    }
    两个程序都这样
      

  2.   

    多次读取,把其中一个去掉就好
     
               while((b=in.read(tom,0,tom.length))!=-1){
                  out.write(tom);
                } 
      

  3.   

    while((b=in.read(tom,0,tom.length))!=-1){
                  out.write(tom, 0, b);
                } 
      

  4.   

    in.read(tom,0,1000); //没必要的代码,你读过一次了!已经游标移到后面去了!