Java读取二进制文件的方式

  1. public static void readFile(String fileName){  
  2.           
  3.             
  4.         File file = new File(fileName);   
  5.         if(file.exists()){  
  6.             try {  
  7.                 FileInputStream in = new FileInputStream(file);  
  8.                 DataInputStream dis=new DataInputStream(in);  
  9.                   
  10.                 byte[] itemBuf = new byte[20];  
  11.                 //市场编码  
  12.                 dis.read(itemBuf, 0, 8);  
  13.                 String marketID =new String(itemBuf,0,8);  
  14.                   
  15.                 //市场名称  
  16.                 dis.read(itemBuf, 0, 20);//read方法读取一定长度之后,被读取的数据就从流中去掉了,所以下次读取仍然从 0开始   
  17.                 String marketName =new String(itemBuf,0,20);  
  18.                   
  19.                 //上一交易日日期  
  20.                 dis.read(itemBuf, 0, 8);  
  21.                 String lastTradingDay = new String(itemBuf,0,8);  
  22.                   
  23.                 //当前交易日日期  
  24.                 dis.read(itemBuf, 0, 8);  
  25.                 String curTradingDay = new String(itemBuf,0,8);  
  26.                   
  27.                 //交易状态  
  28.                 dis.read(itemBuf, 0, 1);  
  29.                 String marketStatus = new String(itemBuf,0,1);  
  30.   
  31.                 //交易时段数  
  32.                 short tradePeriodNum = dis.readShort();  
  33.                   
  34.                 System.out.println("市场代码:"+ marketID);  
  35.                 System.out.println("市场名称:"+ marketName);  
  36.                 System.out.println("上一交易日日期:"+ lastTradingDay);  
  37.                 System.out.println("当前交易日日期:"+ curTradingDay);  
  38.                 System.out.println("当前交易日日期:"+ curTradingDay);  
  39.                 System.out.println("交易状态:"+ marketStatus);  
  40.                 System.out.println("交易时段数:"+ tradePeriodNum);  
  41.   
  42.             } catch (IOException e) {  
  43.                 // TODO Auto-generated catch block  
  44.                 e.printStackTrace();  
  45.             }finally{  
  46.                 //close  
  47.             }  
  48.         }  
  49.     }  
/**
   * 随机读取文件内容
   */
  public static void readFileByRandomAccess(String fileName) {
   RandomAccessFile randomFile = null ;
   try {
    System.out.println( "随机读取一段文件内容:" );
    // 打开一个随机访问文件流,按只读方式
    randomFile = new RandomAccessFile(fileName, "r" );
    // 文件长度,字节数
    long fileLength = randomFile.length();
    // 读文件的起始位置
    int beginIndex = (fileLength > 4 ) ? 4 : 0 ;
    // 将读文件的开始位置移到beginIndex位置。
    randomFile.seek(beginIndex);
    byte [] bytes = new byte [ 10 ];
    int byteread = 0 ;
    // 一次读10个字节,如果文件内容不足10个字节,则读剩下的字节。
    // 将一次读取的字节数赋给byteread
    while ((byteread = randomFile.read(bytes)) != - 1 ) {
     System.out.write(bytes, 0 , byteread);
    }
   } catch (IOException e) {
    e.printStackTrace();
   } finally {
    if (randomFile != null ) {
     try {
      randomFile.close();
     } catch (IOException e1) {
     }
    }
   }
  }

猜你喜欢

转载自www.cnblogs.com/dybk/p/9285011.html