//少写了一点
public Vector readBinaryFile( String filePath )
  {
    Vector biVec = new Vector();
    try
    {
      File biFile = new File( filePath );
      if (biFile.exists())
      {
        FileInputStream biStream = new FileInputStream( biFile );
        int len;
        byte[] biByte = new byte[118];
        while ( ( len = biStream.read( biByte, 0, 118 ) ) != -1 )
        {
          biVec.add( biByte );
        }
      }
    }
    catch( Exception e )
    {
    }    
    return biVec;
  }

解决方案 »

  1.   

    需求是将一个二进制文件读取
    循环将文件中 每 118个字节 存放在Vector中,直到文件结束。
      

  2.   

    public Vector readBinaryFile( String filePath )
      {
        Vector biVec = new Vector();
        try
        {
          File biFile = new File( filePath );
          if (biFile.exists())
          {
            FileInputStream biStream = new FileInputStream( biFile );
            int len;
            byte[] biByte = new byte[118];
            while ( ( len = biStream.read( biByte, 0, 118 ) ) != -1 )
            {
              biVec.add( biByte );
              biByte = new byte[118];
            }
          }
        }
        catch( Exception e )
        {
        }
      

  3.   

    问题很严重;每次执行biVec.add( biByte );的时候,add进去的是同一个对象应用(java的原始类型数组也是对象),所以就算你add了n次,biVec里的所有Element都是同一个对象。改进程序如下:public Vector readBinaryFile( String filePath )
    {
        Vector biVec = new Vector();
        try
        {
          File biFile = new File( filePath );
          if (biFile.exists())
          {
            FileInputStream biStream = new FileInputStream( biFile );
            while(true)
            {
              byte[] biByte = new byte[118];//每次都new一个byte数组来存放读取的数据
              if (biStream.read( biByte) <= 0 )
                 break;
              biVec.add( biByte );
            }
          }
        }
        catch( Exception e )
        {
        }
    }
      

  4.   

    很好!
    不知道要不要这样
    FileInputStream biStream = new FileInputStream(biFile);
              int len;
              int i = 0;
              byte[] biByte = new byte[118];
              while ( (len = biStream.read(biByte, i, 118)) != -1) {
                biVec.add(biByte);
                biByte = new byte[118];
                i = i + 118;
              }
      

  5.   

    muymuy(muy) :
    你的作法很好!
      

  6.   


    来晚了的朋友,可去这里拿分,一样的帖子
    http://expert.csdn.net/Expert/topic/1563/1563178.xml?temp=.594845