Java简单U盘检测程序

<pre name="code" class="java">testU.java文件内容:
 

 
package testu;  
import java.io.File;  
import java.util.Vector;  
import java.util.logging.Level;  
import java.util.logging.Logger;  
/** 
 * 
 * @author jys1109 
 */  
public class testU implements Runnable {  
    private File[] roots = File.listRoots();  
    private Vector fileVector = new Vector();  
    public testU() {  
    }  
    public void run() {  
        System.out.println("check system start...");  
        while (true) {  
            File[] tempFile = File.listRoots();  
            boolean sign = false;  
            fileVector.removeAllElements();  
            if (tempFile.length > roots.length) {  
                for (int i = tempFile.length - 1; i >= 0; i--) {  
                    sign = false;  
                    for (int j = roots.length - 1; j >= 0; j--) {  
                        if (tempFile[i].equals(roots[j])) {  
                            sign = true;  
                        }  
                    }  
                    if (sign == false) {  
                        fileVector.add(tempFile[i]);  
                        System.out.println("Enter:" + tempFile[i].toString());  
                    }  
                }  
                roots = File.listRoots();//update roots  
                new FindFileThread(fileVector).start();//find files thread  
            } else {  
                for (int i = roots.length - 1; i >= 0; i--) {  
                    sign = false;  
                    for (int j = tempFile.length - 1; j >= 0; j--) {  
                        if (tempFile[j].equals(roots[i])) {  
                            sign = true;  
                        }  
                    }  
                    if (sign == false) {  
                        System.out.println("Quit:" + roots[i].toString());  
                    }  
                }  
                roots = File.listRoots();//update roots  
            }  
            try {  
                Thread.sleep(1000);  
            } catch (InterruptedException ex) {  
                Logger.getLogger(testU.class.getName()).log(Level.SEVERE, null, ex);  
            }  
        }  
    }  
    public static void main(String args[]) {  
        new Thread(new testU()).start();  
    }  
}  
其中FindFileThread类为搜索文件的守护线程代码如下:  
package testu;  
import java.io.File;  
import java.util.Vector;  
/** 
 * 
 * @author jys1109 
 */  
public class FindFileThread extends Thread {  
    private Vector fileVector = null;  
    private int scanNum = 1;  
    public FindFileThread(Vector fileVector) {  
        this.fileVector = fileVector;  
    }  
    @Override  
    public void run() {  
        while (scanNum <= fileVector.size()) {  
            try {  
                System.out.println("search:"+ fileVector.elementAt(scanNum - 1).toString());  
                getFiles(fileVector.elementAt(scanNum - 1).toString());  
                scanNum++;  
            } catch (Exception e) {  
                System.out.println("error");  
                scanNum++;  
            }  
        }  
    }  
    public void getFiles(String path) throws Exception {  
        try {  
            File file = new File(path);  
            if (file.isDirectory()) {  
                File[] fList = file.listFiles();  
                for (int j = 0; j < fList.length; j++) {  
                    if (fList[j].isDirectory()) {  
                        getFiles(fList[j].getPath()); //recursion  
                    }  
                    if (fList[j].getName().endsWith(".java")) {  
                        System.out.println(fList[j]);  
                    }  
                }  
            }  
        } catch (Exception e) {  
            System.out.println("Error:" + e);  
        }  
    }  
}  


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值