新聞中心
這篇文章給大家介紹使用java怎么實(shí)時監(jiān)控文件的行尾內(nèi)容,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對大家能有所幫助。

耿馬網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,耿馬網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為耿馬近千家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請找那個售后服務(wù)好的耿馬做網(wǎng)站的公司定做!
1.WatchService
首先介紹一下WatchService類,WatchService可以監(jiān)控某一個目錄下的文件的變動(新增,修改,刪除)并以事件的形式通知文件的變更,這里我們可以實(shí)時的獲取到文件的修改事件,然后計(jì)算出追加的內(nèi)容,Talk is cheap,Show me the code.
Listener
簡單的接口,只有一個fire方法,當(dāng)事件發(fā)生時處理事件。
public interface Listener {
/**
* 發(fā)生文件變動事件時的處理邏輯
*
* @param event
*/
void fire(FileChangeEvent event);
}FileChangeListener
Listener接口的實(shí)現(xiàn)類,處理文件變更事件。
public class FileChangeListener implements Listener {
/**
* 保存路徑跟文件包裝類的映射
*/
private final Map map = new ConcurrentHashMap<>();
public void fire(FileChangeEvent event) {
switch (event.getKind().name()) {
case "ENTRY_MODIFY":
// 文件修改事件
modify(event.getPath());
break;
default:
throw new UnsupportedOperationException(
String.format("The kind [%s] is unsupport.", event.getKind().name()));
}
}
private void modify(Path path) {
// 根據(jù)全路徑獲取包裝類對象
FileWrapper wrapper = map.get(path.toString());
if (wrapper == null) {
wrapper = new FileWrapper(path.toFile());
map.put(path.toString(), wrapper);
}
try {
// 讀取追加的內(nèi)容
new ContentReader(wrapper).read();
} catch (IOException e) {
e.printStackTrace();
}
}
} FileWrapper
文件包裝類,包含文件和當(dāng)前讀取的行號
public class FileWrapper {
/**
* 當(dāng)前文件讀取的行數(shù)
*/
private int currentLine;
/**
* 監(jiān)聽的文件
*/
private final File file;
public FileWrapper(File file) {
this(file, 0);
}
public FileWrapper(File file, int currentLine) {
this.file = file;
this.currentLine = currentLine;
}
public int getCurrentLine() {
return currentLine;
}
public void setCurrentLine(int currentLine) {
this.currentLine = currentLine;
}
public File getFile() {
return file;
}
}FileChangeEvent
文件變更事件
public class FileChangeEvent {
/**
* 文件全路徑
*/
private final Path path;
/**
* 事件類型
*/
private final WatchEvent.Kind> kind;
public FileChangeEvent(Path path, Kind> kind) {
this.path = path;
this.kind = kind;
}
public Path getPath() {
return this.path;
}
public WatchEvent.Kind> getKind() {
return this.kind;
}
}ContentReader
內(nèi)容讀取類
public class ContentReader {
private final FileWrapper wrapper;
public ContentReader(FileWrapper wrapper) {
this.wrapper = wrapper;
}
public void read() throws FileNotFoundException, IOException {
try (LineNumberReader lineReader = new LineNumberReader(new FileReader(wrapper.getFile()))) {
List contents = lineReader.lines().collect(Collectors.toList());
if (contents.size() > wrapper.getCurrentLine()) {
for (int i = wrapper.getCurrentLine(); i < contents.size(); i++) {
// 這里只是簡單打印出新加的內(nèi)容到控制臺
System.out.println(contents.get(i));
}
}
// 保存當(dāng)前讀取到的行數(shù)
wrapper.setCurrentLine(contents.size());
}
}
} DirectoryTargetMonitor
目錄監(jiān)視器,監(jiān)控目錄下文件的變化
public class DirectoryTargetMonitor {
private WatchService watchService;
private final FileChangeListener listener;
private final Path path;
private volatile boolean start = false;
public DirectoryTargetMonitor(final FileChangeListener listener, final String targetPath) {
this(listener, targetPath, "");
}
public DirectoryTargetMonitor(final FileChangeListener listener, final String targetPath, final String... morePaths) {
this.listener = listener;
this.path = Paths.get(targetPath, morePaths);
}
public void startMonitor() throws IOException {
this.watchService = FileSystems.getDefault().newWatchService();
// 注冊變更事件到WatchService
this.path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY);
this.start = true;
while (start) {
WatchKey watchKey = null;
try {
// 阻塞直到有事件發(fā)生
watchKey = watchService.take();
watchKey.pollEvents().forEach(event -> {
WatchEvent.Kind> kind = event.kind();
Path path = (Path) event.context();
Path child = this.path.resolve(path);
listener.fire(new FileChangeEvent(child, kind));
});
} catch (Exception e) {
this.start = false;
} finally {
if (watchKey != null) {
watchKey.reset();
}
}
}
}
public void stopMonitor() throws IOException {
System.out.printf("The directory [%s] monitor will be stop ...\n", path);
Thread.currentThread().interrupt();
this.start = false;
this.watchService.close();
System.out.printf("The directory [%s] monitor will be stop done.\n", path);
}
}測試類
在D盤新建一個monitor文件夾, 新建一個test.txt文件,然后啟動程序,程序啟動完成后,我們嘗試往test.txt添加內(nèi)容然后保存,控制臺會實(shí)時的輸出我們追加的內(nèi)容,PS:追加的內(nèi)容要以新起一行的形式追加,如果只是在原來的尾行追加,本程序不會輸出到控制臺,有興趣的同學(xué)可以擴(kuò)展一下
public static void main(String[] args) throws IOException {
DirectoryTargetMonitor monitor = new DirectoryTargetMonitor(new FileChangeListener(), "D:\\monitor");
monitor.startMonitor();
}關(guān)于使用java怎么實(shí)時監(jiān)控文件的行尾內(nèi)容就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
當(dāng)前文章:使用java怎么實(shí)時監(jiān)控文件的行尾內(nèi)容
鏈接URL:http://www.dlmjj.cn/article/jsopho.html


咨詢
建站咨詢
