Java Utililty Methods RandomAccessFile Read

List of utility methods to do RandomAccessFile Read

Description

The list of methods to do RandomAccessFile Read are organized into topic(s).

Method

byte[]readFile(String file)
read File
return readFile(new File(file));
byte[]readFile(String file)
read File
return readFile(new File(file));
byte[]readFile(String filename)
read File
RandomAccessFile file = new RandomAccessFile(filename, "r");
byte[] buffer = new byte[(int) file.length()];
file.readFully(buffer);
file.close();
return buffer;
StringreadFile(String filePath)
read File
RandomAccessFile raf = new RandomAccessFile(new File(filePath), "r");
String content = raf.readUTF();
return content;
voidreadFileByRandomAccess(String fileName)
read File By Random Access
RandomAccessFile randomFile = null;
try {
    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;
...
byte[]readFileBytes(final File file)
Reads the bytes from the given file if it is a file and it exists.
byte[] data = null;
if (file != null && file.exists() && file.isFile()) {
    RandomAccessFile raf = null;
    try {
        raf = new RandomAccessFile(file, "r");
        data = new byte[(int) raf.length()];
        raf.readFully(data);
    } catch (IOException ex) {
...
byte[]readFileFully(File f)
read File Fully
RandomAccessFile rafile = new RandomAccessFile(f, "r");
int size = (int) rafile.length();
byte[] data = new byte[size];
rafile.readFully(data);
return data;
StringreadLine(RandomAccessFile file, long position, int trim)
Reads a line truncating it as needed.
StringBuilder input = new StringBuilder();
int c = -1;
boolean eol = false;
int count = 0;
file.seek(position);
while (!eol) {
    switch (c = file.read()) {
    case -1:
...
longreadLongLittleEndian(RandomAccessFile randomAccessFile)
read Long Little Endian
final int bytesRead = randomAccessFile.read(BUFFER, 0, BYTES_IN_LONG);
if (bytesRead < 0) {
    return bytesRead;
long value = 0;
for (int i = BYTES_IN_LONG - 1; i >= 0; i--) {
    value = (value << 8) + (BUFFER[i] & 0xffL);
return value;