我想实现,从硬盘读取一个文件,然后存储到字节数组中,请问如何实现,如何把字节内容赋给字节数组?一下使我写的代码:
        File f=new File("d:\\javacode","sample.jpg");
        FileInputStream f1 = new FileInputStream(f);
        
        byte b[]=new byte[f1.available()];

解决方案 »

  1.   

    File f=new File("d:/javacode","sample.jpg");
            FileInputStream f1 = new FileInputStream(f);
            
            byte b[]=new byte[f1.available()];
            int i =0;
            while (f1.readByte()!=-1){
               b[i++] =f1.readByte();
            }
       这样不知道可不可以
         
      

  2.   

    楼上的 readByte() 函数 我怎么没找到 ?
      

  3.   

    import java.io.*;
    import java.io.File;public class ll
    {
    public static void main(String args []) throws IOException
    { File f=new File("d:/","we.txt");
            FileInputStream fi = new FileInputStream(f);
            
            byte b[]=new byte[fi.available()];
            fi.read(b);
         
          
            FileOutputStream fs = new FileOutputStream("out.txt");
            fs.write(b);
            
            fs.close();

    }
    }上面代码把we.txt中的东西写入 b, 然后在由b写入 out.txt 
    搂住可以试一下