java - 如何监视文件中的新内容并检索该内容

标签 java io filesystems diff watchservice

我有一个名为 foo.txt 的文件。该文件包含一些文本。我想实现以下功能:

  1. 我启动程序
  2. 向文件写入一些内容(例如添加一行:new string in foo.txt)
  3. 我只想获取此文件的新内容。

您能阐明这个问题的最佳解决方案吗?另外我想解决相关问题:如果我修改 foo.txt 我想查看差异。

我在 Java 中找到的最接近的工具是 WatchService,但如果我理解正确的话,这个工具只能检测文件系统上发生的事件类型(创建文件或删除或修改)。

最佳答案

Java Diff Utils就是为此目的而设计的。

final List<String> originalFileContents = new ArrayList<String>();
final String filePath = "C:/Users/BackSlash/Desktop/asd.txt";

FileListener fileListener = new FileListener() {

    @Override
    public void fileDeleted(FileChangeEvent paramFileChangeEvent)
    throws Exception {
        // use this to handle file deletion event

    }

    @Override
    public void fileCreated(FileChangeEvent paramFileChangeEvent)
    throws Exception {
        // use this to handle file creation event

    }

    @Override
    public void fileChanged(FileChangeEvent paramFileChangeEvent)
    throws Exception {
        System.out.println("File Changed");
        //get new contents
        List<String> newFileContents = new ArrayList<String> ();
        getFileContents(filePath, newFileContents);
        //get the diff between the two files
        Patch patch = DiffUtils.diff(originalFileContents, newFileContents);
        //get single changes in a list
        List<Delta> deltas = patch.getDeltas();
        //print the changes
        for (Delta delta : deltas) {
            System.out.println(delta);
        }
    }
};

DefaultFileMonitor monitor = new DefaultFileMonitor(fileListener);
try {
    FileObject fileObject = VFS.getManager().resolveFile(filePath);
    getFileContents(filePath, originalFileContents);
    monitor.addFile(fileObject);
    monitor.start();
} catch (InterruptedException ex) {
    ex.printStackTrace();
} catch (FileNotFoundException e) {
    //handle
    e.printStackTrace();
} catch (IOException e) {
    //handle
    e.printStackTrace();
}

哪里getFileContents是:

void getFileContents(String path, List<String> contents) throws FileNotFoundException, IOException {
    contents.clear();
    BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path), "UTF-8"));
    String line = null;
    while ((line = reader.readLine()) != null) {
        contents.add(line);
    }
}

我做了什么:

  1. 我将原始文件内容加载到 List<String> 中.
  2. 我用过Apache Commons VFS监听文件更改,使用 FileMonitor 。您可能会问,为什么?因为WatchService仅从 Java 7 开始可用,而 FileMonitor至少适用于 Java 5(个人喜好,如果您喜欢 WatchService 您可以使用它)。 注意:Apache Commons VFS 依赖于Apache Commons Logging ,您必须将两者添加到构建路径中才能使其正常工作。
  3. 我创建了一个FileListener ,然后我实现了 fileChanged方法。
  4. 该方法从文件中加载新内容,并使用 Patch.diff检索所有差异,然后打印它们
  5. 我创建了一个DefaultFileMonitor ,它基本上监听文件的更改,然后我将我的文件添加到其中。
  6. 我启动了监视器。

监视器启动后,它将开始监听文件更改。

关于java - 如何监视文件中的新内容并检索该内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24301511/

相关文章:

Java7 WatchService - 如何检测实际监视目录的重命名/移动

linux - 轮询就绪文件

java - 我可以使用 java web start 启动 one-jar 吗

java - 使用 Snowball Stemmer 时发生不兼容的类更改错误

java - 从 ArrayList 检索 2D 数组的问题

c++ - 在 Windows 的 C++ 控制台应用程序中重新定义 'pause' 行为

c++ - 确定当前线程是否具有低 I/O 优先级

c++ - 为什么用 C++ 编译文件系统代码会返回这个错误?

c - 为什么 readdir () 系统调用没有按应有的方式工作(意外输出)?

java - 将线程保持在 Spring 休息请求中以进行长轮询