Search in sources :

Example 1 with LineHandler

use of cn.hutool.core.io.LineHandler in project hutool by looly.

the class LineReadWatcher method onModify.

@Override
public void onModify(WatchEvent<?> event, Path currentPath) {
    final RandomAccessFile randomAccessFile = this.randomAccessFile;
    final Charset charset = this.charset;
    final LineHandler lineHandler = this.lineHandler;
    try {
        final long currentLength = randomAccessFile.length();
        final long position = randomAccessFile.getFilePointer();
        if (position == currentLength) {
            // 内容长度不变时忽略此次事件
            return;
        } else if (currentLength < position) {
            // 如果内容变短或变0,说明文件做了删改或清空,回到内容末尾或0
            randomAccessFile.seek(currentLength);
            return;
        }
        // 读取行
        FileUtil.readLines(randomAccessFile, charset, lineHandler);
        // 记录当前读到的位置
        randomAccessFile.seek(currentLength);
    } catch (IOException e) {
        throw new IORuntimeException(e);
    }
}
Also used : LineHandler(cn.hutool.core.io.LineHandler) RandomAccessFile(java.io.RandomAccessFile) IORuntimeException(cn.hutool.core.io.IORuntimeException) Charset(java.nio.charset.Charset) IOException(java.io.IOException)

Aggregations

IORuntimeException (cn.hutool.core.io.IORuntimeException)1 LineHandler (cn.hutool.core.io.LineHandler)1 IOException (java.io.IOException)1 RandomAccessFile (java.io.RandomAccessFile)1 Charset (java.nio.charset.Charset)1