你定义一个大点的byte[]暂存不就得了
读完后不救知道长度了?

解决方案 »

  1.   

    一般都这样作,得到文件名,定义一个定长的byte[],然后循环读文件,直到文件结束为止。
    或者这样也可以。
    long lFileLenth;
    byte filebyte[];
    File myfile=new File(sfile);
    //get the length of file
    lFileLenth=myfile.length();
    //create a buffer for file
    filebyte=new byte[(int)lFileLenth];
    FileInputStream file=new FileInputStream(myfile);
    file.read(filebyte,0,(int)filelenth);
    如果你的目的为了拷贝或者文件很大时,建议使用第一种方法,用循环作。
      

  2.   

    在文件中的开始位置存放文件共有多少byte。
    在读文件时要捕获溢出异常,然后扩大byte[]的大小。try{
       ...
    }catch(...){
       byte[] aTemp = new byte[nLength + nIncrement];
       System.arraycopy(aBytes, 0, aTemp, aBytes.length);
       aBytes = aTemp;   
    }其实Vector类就是类似这样做的: