Example usage for java.io RandomAccessFile readByte

List of usage examples for java.io RandomAccessFile readByte

Introduction

In this page you can find the example usage for java.io RandomAccessFile readByte.

Prototype

public final byte readByte() throws IOException 

Source Link

Document

Reads a signed eight-bit value from this file.

Usage

From source file:com.peterbochs.PeterBochsDebugger.java

public String tail2(File file, int lines) {
    try {//from w w  w.j av a 2  s . c om
        RandomAccessFile fileHandler = new RandomAccessFile(file, "r");
        long fileLength = file.length() - 1;
        StringBuilder sb = new StringBuilder();
        int line = 0;

        for (long filePointer = fileLength; filePointer != -1; filePointer--) {
            fileHandler.seek(filePointer);
            int readByte = fileHandler.readByte();

            if (readByte == 0xA) {
                line = line + 1;
                if (line == lines) {
                    if (filePointer == fileLength) {
                        continue;
                    } else {
                        break;
                    }
                }
            }
            sb.append((char) readByte);
        }

        sb.deleteCharAt(sb.length() - 1);
        String lastLine = sb.reverse().toString();
        return lastLine;
    } catch (Exception e) {
        return null;
    }
}