Class TailReader

java.lang.Object
cloud.opencode.base.io.file.TailReader

public final class TailReader extends Object
Tail Reader Utility 尾部读取器工具

Efficient utility for reading the last N lines or bytes of a file using backward scanning with RandomAccessFile. Avoids reading the entire file into memory.

使用RandomAccessFile反向扫描高效读取文件末尾N行或N字节的工具。 避免将整个文件读入内存。

Features | 主要功能:

  • Read last N lines efficiently - 高效读取最后N行
  • Read last N bytes - 读取最后N个字节
  • Configurable charset - 可配置的字符集
  • Block-based backward scanning - 基于块的反向扫描

Usage Examples | 使用示例:

// Read last 10 lines (UTF-8)
List<String> lines = TailReader.lastLines(path, 10);

// Read last 10 lines with specific charset
List<String> lines = TailReader.lastLines(path, 10, StandardCharsets.ISO_8859_1);

// Read last 1024 bytes
byte[] tail = TailReader.lastBytes(path, 1024);

Limitations | 限制:

  • Line detection relies on newline byte (0x0A). This works reliably for single-byte charsets and UTF-8, but may not work correctly for charsets where 0x0A can appear as part of a multi-byte sequence (e.g., some legacy CJK encodings). - 行检测依赖换行字节(0x0A)。这对于单字节字符集和UTF-8可靠工作, 但对于0x0A可能出现在多字节序列中的字符集可能无法正确工作。

Security | 安全性:

  • Thread-safe: Yes (stateless utility) - 线程安全: 是(无状态工具)
Since:
JDK 25, opencode-base-io V1.0.3
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • lastLines

      public static List<String> lastLines(Path path, int count)
      Reads the last N lines of a file using UTF-8 charset 使用UTF-8字符集读取文件的最后N行
      Parameters:
      path - the file path | 文件路径
      count - the number of lines to read | 要读取的行数
      Returns:
      the last N lines (or fewer if file has less) | 最后N行(如果文件行数不足则返回全部)
      Throws:
      IllegalArgumentException - if count is not positive | 当count不为正数时抛出
      NullPointerException - if path is null | 当path为null时抛出
      OpenIOOperationException - if an I/O error occurs | 当发生I/O错误时抛出
    • lastLines

      public static List<String> lastLines(Path path, int count, Charset charset)
      Reads the last N lines of a file with the specified charset 使用指定字符集读取文件的最后N行
      Parameters:
      path - the file path | 文件路径
      count - the number of lines to read | 要读取的行数
      charset - the charset to decode the file | 解码文件的字符集
      Returns:
      the last N lines (or fewer if file has less) | 最后N行(如果文件行数不足则返回全部)
      Throws:
      IllegalArgumentException - if count is not positive | 当count不为正数时抛出
      NullPointerException - if path or charset is null | 当path或charset为null时抛出
      OpenIOOperationException - if an I/O error occurs | 当发生I/O错误时抛出
    • lastBytes

      public static byte[] lastBytes(Path path, int count)
      Reads the last N bytes of a file 读取文件的最后N个字节
      Parameters:
      path - the file path | 文件路径
      count - the number of bytes to read | 要读取的字节数
      Returns:
      the last N bytes (or fewer if file is smaller) | 最后N个字节(如果文件更小则返回全部)
      Throws:
      IllegalArgumentException - if count is not positive | 当count不为正数时抛出
      NullPointerException - if path is null | 当path为null时抛出
      OpenIOOperationException - if an I/O error occurs | 当发生I/O错误时抛出