Code example for RandomAccessFile

Methods: close, length, readFully

0
  public static byte[] readFile(String file) throws IOException {
    return readFile(new File(file));
  } 
 
  public static byte[] readFile(File file) throws IOException {
    RandomAccessFile f = new RandomAccessFile(file, "r");
 
    try { 
      long longlength = f.length();
      int length = (int) longlength;
      if (length != longlength) {
        throw new IOException("File size >= 2 GB");
      } 
 
      byte[] data = new byte[length];
      f.readFully(data);
      return data;
    } finally { 
      f.close();
    } 
  } 
 
  public static String toHexString(byte[] data) {
    return toHexString(data, 0, data.length);