java 循环读取文件_您如何用Java连续读取文件?

本文介绍了一种使用java.io.RandomAccessFile来循环读取文件的方法。通过不断检查文件长度并定位读取位置,实现实时监控文件更新,当有新内容时发送通知。示例代码展示了一个LogFileTailer类,它可以监听文件变化并通知监听者。
摘要由CSDN通过智能技术生成

小编典典

这有点旧,但是我已经使用了该机制,并且效果很好。

诀窍是使用java.io.RandomAccessFile和,并定期检查文件长度是否大于当前文件位置。如果是,则读取数据。当您达到长度时,您将等待。洗涤,漂洗,重复。

我复制了代码,以防万一新链接停止工作

package com.javasrc.tuning.agent.logfile;

import java.io.*;

import java.util.*;

/**

* A log file tailer is designed to monitor a log file and send notifications

* when new lines are added to the log file. This class has a notification

* strategy similar to a SAX parser: implement the LogFileTailerListener interface,

* create a LogFileTailer to tail your log file, add yourself as a listener, and

* start the LogFileTailer. It is your job to interpret the results, build meaningful

* sets of data, etc. This tailer simply fires notifications containing new log file lines,

* one at a time.

*/

public class LogFileTailer extends Thr

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值