监控某一个小文件夹的java程序

来源:互联网 发布:diy编程器论坛 编辑:程序博客网 时间:2024/05/16 14:16

以下是原代码 ,用于监控一个小的文件夹的变化

   package watchproject;


import java.io.File;


import java.util.HashMap;


import java.util.Iterator;


import java.util.Map;


import java.util.Set;


/**
 * 
 * <p>
 * 
 * Description:监控某个文件路径下的变化
 * 
 * </p>
 * 
 * @author imasmallbird
 * 
 * @version $Revision 1.1 $ 2009-10-10 下午01:14:26
 */


public class DirMonitor implements Runnable {


/**

* 监控的文件路径
*/


private String dir;


/**

* 扫描间隔时间以秒为单位
*/


private int period;


/**

* 原有文件信息
*/


private Map<String, String> oldDirFileMap;


/**

* 初始化相关数据
*/


public DirMonitor(String dir, int period) {


this.dir = dir;


this.period = period;


this.oldDirFileMap = new HashMap<String, String>();


}


/**

* 线程的执行。对于修改文件的情况,则视为删除与增加两个操作。
*/


public void run() {


boolean isError = false;


File file = new File(dir);


// 初始化开始监控时的文件路径状态


totalScan(file, oldDirFileMap);


// 展示原有路径下的文件信息


displayNowFile();


while (!isError) {


try {


Thread.sleep(period * 1000);


// 指定时间间间隔后扫描路径


Map<String, String> nowDirFileMap = new HashMap<String, String>();


totalScan(file, nowDirFileMap);


// 得到删除的文件及文件夹


getDeletedFile(nowDirFileMap);


// 得到新增的文件及文件夹


getAddedFile(nowDirFileMap);


// 注意:由于涉及到修改,所以一定要先检测删除的文件,然后再检测新增加的文件


} catch (InterruptedException e) {


System.out.println("对指定的文件路径进行监控时产生异常,异常信息为:" + e.getMessage());


isError = true;


}


}


}


/**

* 递归扫描整个路径



* @param file

*            要扫描的路径

* @param dirFileMap

*            存放扫描结果
*/


private void totalScan(File file, Map<String, String> dirFileMap) {


String[] fileList = file.list();


// 判断是否为空目录


if (null != fileList) {


for (int i = 0; i < fileList.length; i++) {


String pname = file.getAbsolutePath() + "\\" + fileList[i];


File tempFile = new File(pname);


if (tempFile.isDirectory()) {


dirFileMap


.put("文件夹:" + pname, tempFile.lastModified() + "");// 修改了此处


totalScan(tempFile, dirFileMap);


} else {


// 不相同的文件夹下,存放的文件可能名字相同,但是同一路径下的文件肯定不会相同,


// 所以采用全路径做为key值


dirFileMap.put("文件:" + pname, tempFile.lastModified() + "");// 修改了此处


}


}


}


}


/**

* 得到增加的文件及文件夹,并增加到已有的文件信息中
*/


private void getAddedFile(Map<String, String> nowDirFileMap) {


for (Iterator<String> iterator = nowDirFileMap.keySet().iterator(); iterator


.hasNext();) {


String key = iterator.next();


String oldValue = oldDirFileMap.get(key);


String newValue = nowDirFileMap.get(key);


if (null == oldValue) {


oldDirFileMap.put(key, newValue);


System.out.println("新增的" + key);


}


}


}


/**

* 得到删除的文件及文件夹,并删除已经不存在的文件信息
*/


private void getDeletedFile(Map<String, String> nowDirFileMap) {


for (Iterator<String> iterator = oldDirFileMap.keySet().iterator(); iterator


.hasNext();) {


String key = iterator.next();


String oldValue = oldDirFileMap.get(key);


String newValue = nowDirFileMap.get(key);


if (null == nowDirFileMap.get(key)) {


System.out.println("删除的" + key);


iterator.remove();


oldDirFileMap.remove(key);


} else if (!newValue.equals(oldValue)) {// 修改了此处


System.out.println("修改的" + key);


oldDirFileMap.put(key, newValue);


}


}


}


/**

* 展示原有文件
*/


private void displayNowFile() {


System.out.println(dir + "路径原有文件目录如下:\n");


Set set = oldDirFileMap.keySet();


Iterator<String> iterator = set.iterator();


while (iterator.hasNext()) {


System.out.println(iterator.next());// 此处修改


}


System.out.println("========================");


}


/**

* just for test

* @param args
*/


public static void main(String[] args) {


DirMonitor dirMonitor = new DirMonitor("D:\\temp", 1);


dirMonitor.run();


}


}

或者用以下的两个类来进行


package watchproject;


public abstract class RunThread implements Runnable


{





    private int second;


    private String filePath;


    Thread runner;


/**


* @param second   时间间隔


* @param filePath 文件路径


*/


public RunThread(int second,String filePath)


    {


    this.second = second*1000;


    this.filePath = filePath;


    }


public void onStart()


    {


    runner = new Thread(this);


    runner.start();


    }


public void run() {


// TODO Auto-generated method stub


Thread.currentThread().setPriority(Thread.MIN_PRIORITY);


while(true)


{


try {


watch(filePath);


Thread.sleep(1000);


} catch (InterruptedException e) {


// TODO Auto-generated catch block


e.printStackTrace();


}


}





}


public abstract void watch(String file);






}


实例类


package watchproject;


import java.io.File;


import java.util.HashMap;


import java.util.Iterator;


import java.util.Map;


import java.util.Vector;


/**
 * 
 * 监控文件目录
 * 
 * @author han
 * 
 * @date 2009-10-10
 */


public class MonitorDir extends RunThread


{


public Map prevFiles = new HashMap();// 存储原来文件路径和最后修改时间


public Map currentFiles = new HashMap();// 存储当前文件路径和最后修改时间


public Map tmpCurrFiles = new HashMap();


public Map tmpPrevFiles = new HashMap();


public static Vector vList = new Vector();


public Vector vtList = new Vector();


/**

* @param second
*            定时扫描间隔

* @param filePath
*            目录路径
*/


public MonitorDir(int second, String filePath)


{


super(second, filePath);


File dirFile = new File(filePath);


if (dirFile.isDirectory())


{


System.out.println("start to watch " + dirFile.getAbsolutePath());


}


}


/**

* 获取文件的信息

* @param dirPath
*/


public void getFilesInfo(String dirPath)


{


File dirFile = new File(dirPath);


prevFiles.clear();


prevFiles.putAll(currentFiles);


currentFiles.clear();


File[] fileList = dirFile.listFiles();


for (int i = 0; i < fileList.length; i++)


{


File tmpFile = fileList[i];


if (tmpFile.isFile())// 文件


{


currentFiles.put(tmpFile.getAbsolutePath(),
tmpFile.lastModified());


}


else// 子目录则递归


{


String tmpPath = tmpFile.getAbsolutePath();


getFilesInfo(tmpPath);


}


}


}


public void addFile(String file)


{


System.out.println(file + " is add");


}


public void changed(String file)


{


System.out.println(file + " is changed");


}


public void delete(String file)


{


System.out.println(file + " is delete");


}


/*

* 监控
*/


public void watch(String dirPath)


{
getFilesInfo(dirPath);


Iterator currentIt = currentFiles.keySet().iterator();


while (currentIt.hasNext())


{


String filePath = (String) currentIt.next();


Long currentModify = (Long) currentFiles.get(filePath);


if (!prevFiles.containsKey(filePath))// 假如原来的hashmap中不存在当前键,则为增加的


{


addFile(filePath);


// if()


}


else if (prevFiles.containsKey(filePath))


{


Long prevModify = (Long) prevFiles.get(filePath);


if (prevModify.compareTo(currentModify) != 0)// 最后修改时间不同,则为改变的


{


changed(filePath);


}


}


// System.out.println("当前------"+filePath+","+currentModify);


}


Iterator prevIt = prevFiles.keySet().iterator();


while (prevIt.hasNext())


{


String prevFilePath = prevIt.next().toString();


if (!currentFiles.containsKey(prevFilePath))// 原来的键不在当前hashmap中,则为删除的


{


delete(prevFilePath);


}


// System.out.println("原来------"+prevFilePath+","+tmpPrevFiles.get(prevFilePath));


}


}


public static void main(String args[])


{


MonitorDir md = new MonitorDir(5, "D:/temp/");


md.onStart();


}


}

0 0