读二进制文件

来源:互联网 发布:阜阳数据库工程师招聘 编辑:程序博客网 时间:2024/05/16 17:37
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
  public static void main(String[] args) {
    File aFile = new File("C:/test.bin");
    FileInputStream inFile = null;
    try {
      inFile = new FileInputStream(aFile);
    catch (FileNotFoundException e) {
      e.printStackTrace(System.err);
    }
    FileChannel inChannel = inFile.getChannel();
    final int PRIMECOUNT = 6;
    ByteBuffer buf = ByteBuffer.allocate(* PRIMECOUNT);
    long[] primes = new long[PRIMECOUNT];
    try {
      while (inChannel.read(buf!= -1) {
        ((ByteBuffer) (buf.flip())).asLongBuffer().get(primes);
        for (long prime : primes) {
          System.out.printf("%10d", prime);
        }
        buf.clear();
      }
      inFile.close();
    catch (IOException e) {
      e.printStackTrace(System.err);
    }
  }
}