`
oboaix
  • 浏览: 269308 次
社区版块
存档分类
最新评论

学习Jnotify文件监视

    博客分类:
  • JAVA
 
阅读更多



 学习Jnotify文件监视用例

 

研究文件监视(Windows),对文件的增加、修改、重命名、删除做记录,找到Java开源技术Jnotify,做了稍微学习,留下一点记录,以资备用,网络上资料有限。

 

 

package com.jnotify;

import net.contentobjects.jnotify.JNotify;

/**
 * Monitor file directory file (folder ) is created, modified, deleted, renamed files[folders]
 * (To adapt to a sub-folders).
 * JDK: 1.6.0_19
 * JAR: jnotify-0.94.jar
 * @author Dennis Zhao
 * @createdTime:2012-09-28
 * Technology from website: http://jnotify.sourceforge.net/
 */

public class JNotifyTest {

    public static void main(String[] args) {
        JNotifyTest test = new JNotifyTest();
        try {
            test.sample();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void sample() throws Exception {
        // path to watch
        String path = "D:\\abc";
        // watch mask, specify events you care about,
        // or JNotify.FILE_ANY for all events.
        int mask = JNotify.FILE_CREATED | JNotify.FILE_DELETED | JNotify.FILE_MODIFIED | JNotify.FILE_RENAMED;
        // watch subtree?
        boolean watchSubtree = true;
        // add actual watch
        int watchID = JNotify.addWatch(path, mask, watchSubtree, new Listener());
        // sleep a little, the application will exit if you
        // don't (watching is asynchronous), depending on your
        // application, this may not be required
        Thread.sleep(15000);
        //Thread.sleep(5000);
        // to remove watch the watch
        boolean res = JNotify.removeWatch(watchID);
        if (!res) {
            // invalid watch ID specified.
            System.out.println("Delete from data : " + watchID);
        }
    }

}

 

 

package com.jnotify;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import net.contentobjects.jnotify.JNotifyListener;

/**
 * Listener implement
 * JDK: 1.6.0_19
 * JAR: jnotify-0.94.jar
 * @author Dennis Zhao
 * @createdTime:2012-09-28
 * Technology from website: http://jnotify.sourceforge.net/
 */
public class Listener implements JNotifyListener {

    public void fileRenamed(int wd, String rootPath, String oldName, String newName) {
        writeTextAppend("Renamed==1==File path== [" + rootPath + File.separator + oldName + "] --> [" + rootPath + File.separator + newName + "]==time==" + System.currentTimeMillis());
        //print("Renamed " + rootPath + " : " + oldName + " -> " + newName);
        operateDataDB2(wd,rootPath + File.separator + newName,"Renamed",rootPath,rootPath + File.separator + oldName,rootPath + File.separator + newName);
    }

    public void fileModified(int wd, String rootPath, String name) {
        writeTextAppend("Modified==2==File path==  [" + rootPath + File.separator + name + "]==time==" + System.currentTimeMillis());
        //print("Modified " + rootPath + " : " + name);
        operateDataDB2(wd,rootPath + File.separator + name,"Modified",rootPath,"","");
    }

    public void fileDeleted(int wd, String rootPath, String name) {
        writeTextAppend("Deleted==3==File path==  [" + rootPath + File.separator + name + "]==time==" + System.currentTimeMillis());
        //print("Deleted " + rootPath + " : " + name);
        operateDataDB2(wd,rootPath + File.separator + name,"Deleted",rootPath,"","");
    }

    public void fileCreated(int wd, String rootPath, String name) {
        writeTextAppend("Created==4==File path==  [" + rootPath + File.separator + name + "]==time==" + System.currentTimeMillis());
        //print("Created " + rootPath + " : " + name);
        operateDataDB2(wd,rootPath + File.separator + name,"Created",rootPath,"","");
    }

    void print(String msg) {
        System.out.println(msg);
    }

    /**
     *
     * writeText
     * @param record
     * @return the void
     */
    private void writeTextAppend(final String record) {
        try {
            FileWriter writer = new FileWriter("d:\\File_log.txt", true);
            writer.write(record + "\n");
            writer.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     *
     * oracle database test
     * operateDataOracle
     * @param wd
     * @param operatName
     * @param operateType
     * @param rootPath
     * @param oldName
     * @param newName
     * @return  void
     */
    private void operateDataOracle(int wd, String operatName, String operateType, String rootPath, String oldName, String newName) {
        Connection conn = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = java.sql.DriverManager.getConnection("jdbc:oracle:thin:@10.199.130.221:1522:ORCL", "scott", "tiger");
            String sql = "insert into t_file_log (ID, OP_NAME, OP_DATETIME, OP_ID, OP_TYPE, ROOT_PATH, OLD_NAME, NEW_NAME) "
                + "values (seq_file_log.nextval, ?, sysdate, ?, ?, ?, ?, ?)";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, operatName);
            pst.setInt(2, wd);
            pst.setString(3, operateType);
            pst.setString(4, rootPath);
            pst.setString(5, oldName);
            pst.setString(6, newName);
            pst.executeUpdate();
            pst.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     *
     * DB2 database test
     * operateDataDB2
     * @param wd
     * @param operatName
     * @param operateType
     * @param rootPath
     * @param oldName
     * @param newName
     * @return  void
     */
    private void operateDataDB2(int wd, String operatName, String operateType, String rootPath, String oldName, String newName) {
        Connection conn = null;
        try {
            Class.forName("com.ibm.db2.jcc.DB2Driver");
            conn = java.sql.DriverManager.getConnection(
                "jdbc:db2://10.199.30.249:50000/DB902:currentSchema=ORCL;", "SCOTT", "TIGER");
            String sql = "insert into TRSUAT.t_file_log (ID, OP_NAME, OP_DATETIME, OP_ID, OP_TYPE, ROOT_PATH, OLD_NAME, NEW_NAME) "
                + "values (seq_file_log.nextval, ?, sysdate, ?, ?, ?, ?, ?)";
            PreparedStatement pst = conn.prepareStatement(sql);
            pst.setString(1, operatName);
            pst.setInt(2, wd);
            pst.setString(3, operateType);
            pst.setString(4, rootPath);
            pst.setString(5, oldName);
            pst.setString(6, newName);
            pst.executeUpdate();
            pst.close();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

 

 

 

注意地方:

增加启动命令项,

java -Djava.library.path=. -jar jnotify-VER.jar [dir]

这里有如图Ecplise下面简单设置,如下图

 

 

 

 

 如需要实际业务操作,根据情况做适当调整。

 

  • 描述: java -Djava.library.path 简单设置
  • 大小: 129.7 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics