java實時監控檔案行尾內容的實現
阿新 • • 發佈:2020-02-11
今天講一下怎樣用Java實現實時的監控檔案行尾的追加內容,類似Linux命令
tail -f
在之前的面試中遇到過一個問題,就是用Java實現tail功能,之前的做法是做一個定時任務每隔1秒去讀取一次檔案,去判斷內容是否有追加,如果有則輸出新追加的內容,這個做法雖然能勉強實現功能,但是有點太low,今天採用另外一種實現方式,基於事件通知。
1.WatchService
首先介紹一下WatchService類,WatchService可以監控某一個目錄下的檔案的變動(新增,修改,刪除)並以事件的形式通知檔案的變更,這裡我們可以實時的獲取到檔案的修改事件,然後計算出追加的內容,Talk is cheap,Show me the code.
Listener
簡單的介面,只有一個fire方法,當事件發生時處理事件。
public interface Listener { /** * 發生檔案變動事件時的處理邏輯 * * @param event */ void fire(FileChangeEvent event); }
FileChangeListener
Listener介面的實現類,處理檔案變更事件。
public class FileChangeListener implements Listener { /** * 儲存路徑跟檔案包裝類的對映 */ private final Map<String,FileWrapper> 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) { // 根據全路徑獲取包裝類物件 FileWrapper wrapper = map.get(path.toString()); if (wrapper == null) { wrapper = new FileWrapper(path.toFile()); map.put(path.toString(),wrapper); } try { // 讀取追加的內容 new ContentReader(wrapper).read(); } catch (IOException e) { e.printStackTrace(); } } }
FileWrapper
檔案包裝類,包含檔案和當前讀取的行號
public class FileWrapper { /** * 當前檔案讀取的行數 */ private int currentLine; /** * 監聽的檔案 */ 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
內容讀取類
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<String> contents = lineReader.lines().collect(Collectors.toList()); if (contents.size() > wrapper.getCurrentLine()) { for (int i = wrapper.getCurrentLine(); i < contents.size(); i++) { // 這裡只是簡單打印出新加的內容到控制檯 System.out.println(contents.get(i)); } } // 儲存當前讀取到的行數 wrapper.setCurrentLine(contents.size()); } } }
DirectoryTargetMonitor
目錄監視器,監控目錄下檔案的變化
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 { // 阻塞直到有事件發生 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新增內容然後儲存,控制檯會實時的輸出我們追加的內容,PS:追加的內容要以新起一行的形式追加,如果只是在原來的尾行追加,本程式不會輸出到控制檯,有興趣的同學可以擴充套件一下
public static void main(String[] args) throws IOException { DirectoryTargetMonitor monitor = new DirectoryTargetMonitor(new FileChangeListener(),"D:\\monitor"); monitor.startMonitor(); }
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支援我們。