Here you can find the source of readFileByRandomAccess(String fileName)
public static void readFileByRandomAccess(String fileName)
//package com.java2s; //License from project: Apache License import java.io.IOException; import java.io.RandomAccessFile; public class Main { public static void readFileByRandomAccess(String fileName) { RandomAccessFile randomFile = null; try {// w w w . ja v a2 s. c o m randomFile = new RandomAccessFile(fileName, "r"); long fileLength = randomFile.length(); int beginIndex = (fileLength > 4) ? 4 : 0; randomFile.seek(beginIndex); byte[] bytes = new byte[10]; int byteread = 0; 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) { } } } } }