Java monitors the file in real time, if the file is modified, get the file update content by line each time

I. Overview:

Use java to realize the monitoring function of the file. When the file is modified, the updated content will be obtained in real time.

1. Requires real-time monitoring of target files

2. Read the file by line and update the line content

3. Put the obtained content into the warehouse

Second, the use of technology:

2.1 commons-io

Use the related classes under the monitor of Commons-io to handle the monitoring of files, which is implemented by the observer mode

  • (1) Can monitor the creation, deletion and modification of folders
  • (2) Can monitor the creation, deletion and modification of files
  • (3) It is realized by the observer mode
  • (4) Use threads to regularly refresh the changes in the detection files

2.2 RandomAccessFile

The only parent class of RandomAccessFile is Object, unlike other stream parent classes. RandomAccessFile is usually used to access files that save data records. It provides a seek( ) method to access records, and can read and write from the record position. The size of these records does not have to be the same; but their size and position must be known.

3. Specific steps

3.1 Introduce the commons-io package, which requires 2.0 or higher

<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
  <groupId>commons-io</groupId>
  <artifactId>commons-io</artifactId>
  <version>2.6</version>
</dependency>

3.2 Write a class FileListener that inherits from FileAlterationListenerAdaptor

package utils;
import java.io.File;
import java.io.IOException;

import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationObserver;


public class FileListener extends FileAlterationListenerAdaptor {
    ReadTest readTest = new ReadTest();
    /**
     * 文件创建执行
     */
    public void onFileCreate(File file) {
        //也可以打印日志
        System.out.println("文件[新建]:" + file.getAbsolutePath());
    }
    /**
     * 文件创建修改
     */
    public void onFileChange(File file) {
        System.out.println("文件[修改]:" + file.getAbsolutePath());
         //目标文件路径
        String str = "d:\\test\\est.txt";
        //如果被修改的是目标文件,执行自己的业务操作
        if(str.equals(file.getAbsolutePath())){
            try {
                readTest.readAW();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    /**
     * 文件删除
     */
    public void onFileDelete(File file) {

        System.out.println("文件[删除]:" + file.getAbsolutePath());
    }
    /**
     * 目录创建
     */
    public void onDirectoryCreate(File directory) {

        System.out.println("[新建]:" + directory.getAbsolutePath());
    }
    /**
     * 目录修改
     */
    public void onDirectoryChange(File directory) {
        System.out.println("[修改]:" + directory.getAbsolutePath());
    }
    /**
     * 目录删除
     */
    public void onDirectoryDelete(File directory) {

        System.out.println("[删除]:" + directory.getAbsolutePath());
    }
    public void onStart(FileAlterationObserver observer) {
        // TODO Auto-generated method stub
        super.onStart(observer);
    }
    public void onStop(FileAlterationObserver observer) {
        // TODO Auto-generated method stub
        super.onStop(observer);
    }
}
  • The principle of file monitoring is as follows:
  • The thread in the file monitoring class FileAlterationMonitor continuously scans the file to observe the FileAlterationObserver . If there is a file change, it judges whether the file is added, deleted, or changed according to the relevant file comparator. (The default is 1000 milliseconds to perform a scan)

3.3 Implement the main method

public static void main(String[] args) throws Exception{
    // 监控目录
    String rootDir = "D://test";
    // 轮询间隔
    long interval = TimeUnit.SECONDS.toMillis(1);
    // 创建过滤器
    IOFileFilter directories = FileFilterUtils.and(
        // 只监听文件夹、目录
        FileFilterUtils.directoryFileFilter(),
        // 此过滤器接受File隐藏的
        HiddenFileFilter.VISIBLE);
    IOFileFilter files    = FileFilterUtils.and(
        // 只监听文件
        FileFilterUtils.fileFileFilter(),
        // 只监听文件后缀为txt的文件
        FileFilterUtils.suffixFileFilter(".txt"));
    IOFileFilter filter = FileFilterUtils.or(directories, files);
    // 使用过滤器
    FileAlterationObserver observer = new FileAlterationObserver(new File(rootDir), filter);
    //不使用过滤器
    //FileAlterationObserver observer = new FileAlterationObserver(new File(rootDir));
    observer.addListener(new FileListener());
    //创建文件变化监听器
    FileAlterationMonitor monitor = new FileAlterationMonitor(interval, observer);
    // 开始监控
    monitor.start();
  }

So far, file monitoring is complete. Now start the main() method, and then operate on the target file. When the listener detects that the target file is modified, it will execute the specified operation in onFileChange() under the FileListener class.

Next, you need to use RandomAccessFile to read text and update content by line

3.4 Implement readAW() (own business operation)

public class ReadTest {
    //设置变量 记录上次读取位置
    private long num = 0;

    public void readAW() throws IOException {
        File file = new File("d://test//est.txt");
        RandomAccessFile randomAccessFile = new RandomAccessFile(file,"rw");
        //将文件定位到偏移量所指位置,在该位置发生下一个读取或写入操作
        randomAccessFile.seek(num);
        //获取按行读取的数据并落库
        String s = randomAccessFile.readLine();
        for(;s!= null;s = randomAccessFile.readLine()){
            User user = new User();
            user.setUserName(s);
            userServer.insertUser(user);
        }
        //重新计算偏移量,做下一次读取时的初始偏移量
        num= randomAccessFile.length();
    }
}

Note: This way of writing can only get the content appended to the end of the file for each update, and the modification of the previous content of the file is unknown

3.5 Effect display

  • View the target file and database, initially empty

     

     

  • Write a line of data to the file and save it

     

  • Check the database and find that the storage is successful

     

  • Add a few more pieces of data and find that the basis is successfully dropped

     

     

 

Guess you like

Origin blog.csdn.net/qq_56044050/article/details/124214228