Java RandomAccessFile Read readFileByRandomAccess(String fileName)

Here you can find the source of readFileByRandomAccess(String fileName)

Description

read File By Random Access

License

Apache License

Declaration

public static void readFileByRandomAccess(String fileName) 

Method Source Code

//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) {
                }
            }
        }
    }
}

Related

  1. readFile(String absoluteFileName)
  2. readFile(String file)
  3. readFile(String file)
  4. readFile(String filename)
  5. readFile(String filePath)
  6. readFileBytes(final File file)
  7. readFileFully(File f)
  8. readLine(RandomAccessFile file, long position, int trim)
  9. readLongLittleEndian( RandomAccessFile randomAccessFile)