转载请注明出处:http://blog.csdn.net/l1028386804/article/details/53439922
今天,给大家带来一篇由Java实现U盘监测和文件递归的文章,其中代码主要通过File类中的listroots对文件系统进行遍历,比较出盘符的变化,进而通过递归遍历出U盘中的内容。好了,不多说了,我们直接上代码
package com.lyz.disk.test;
import java.io.File;
import java.util.Vector;
/**
* 搜索文件系统的盘符
* @author liuyazhuang
*
*/
public class DiskSearchThread implements Runnable {
/** root 现有文件系统的盘符 */
private File[] roots = File.listRoots();
/** fileVector 为了遍历U盘内文件 */
private VectorfileVector = new Vector();
volatile boolean sign = false;
SearchFileThread t = null;
public DiskSearchThread() {
}
@Override
public void run() {
System.out.println("Checking System...");
while (true) {
File[] tempFiles = File.listRoots();
fileVector.removeAllElements();
/** 检测到了有U盘插入 */
if (tempFiles.length > roots.length) {
for (int i = tempFiles.length - 1; i >= 0; i--) {
sign = false;
for (int j = roots.length - 1; j >= 0; j--) {
/** 如果前后比较的盘符相同 */
if (tempFiles[i].equals(roots[j])) {
sign = true;
}
}
/** 如果前后比较的盘符不相同,将不相同的盘符写入向量,并做进一步处理 */
if (!sign) {
fileVector.add(tempFiles[i]);
}
}
roots = File.listRoots();
t = new SearchFileThread(fileVector);
t.start();
} else {
for (int i = roots.length - 1; i >= 0; i--) {
sign = false;
for (int j = tempFiles.length - 1; j >= 0; j--) {
if (tempFiles[j].equals(roots[i])) {
sign = true;
}
}
/** 如果前后比较的盘符不相同,表明U盘被拔出 */
if (!sign) {
System.out.println("QUIT:" + roots[i].toString());
fileVector.removeAllElements();
t.setIsExistToFalse();
// roots=File.listRoots();
}
}
roots = File.listRoots();
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new Thread(new DiskSearchThread()).start();
}
}
package com.lyz.disk.test;
import java.io.File;
import java.util.Vector;
/**
* 搜索文件的线程
* @author liuyazhuang
*
*/
public class SearchFileThread extends Thread {
private VectorfileVector = null;
private int scanNum = 1;
/** 线程安全的变量,用于退出线程 */
volatile boolean isExist = true;
public SearchFileThread(VectorfileVector) {
this.fileVector = fileVector;
System.out.println("fileVector size:" + fileVector.size());
}
@Override
public void run() {
File file = fileVector.elementAt(scanNum - 1);
long totalMemory = file.getFreeSpace();
while (isExist) {
while (scanNum <&#61; fileVector.size()) {
try {
System.out.println("search:"
&#43; fileVector.elementAt(scanNum - 1).toString()
&#43; " Total Space:"
&#43; fileVector.elementAt(scanNum - 1).getTotalSpace()
/ 1024 / 1024 &#43; "MB Free Space:"
&#43; fileVector.elementAt(scanNum - 1).getFreeSpace()
/ 1024 / 1024 &#43; "MB");
/** 遍历文件内容 */
getFiles(fileVector.elementAt(scanNum - 1).getPath());
scanNum&#43;&#43;;
} catch (Exception e) {
e.printStackTrace();
scanNum&#43;&#43;;
}
}
/** 如果盘符的大小发生变化&#xff0c;则有文件进出 */
if (totalMemory !&#61; file.getFreeSpace()) {
System.out.println("文件发生变化----------------------");
getFiles(file.getPath());
totalMemory &#61; file.getFreeSpace();
}
}
}
/**
* 递归遍历文件
* &#64;param path
*/
public void getFiles(String path) {
try {
File file &#61; new File(path);
if (file.isDirectory()) {
File[] list &#61; file.listFiles();
for (int i &#61; 0; i if (list[i].isDirectory()) {
/** 递归调用 */
getFiles(list[i].getPath());
}
System.out.println("Find File:" &#43; list[i].getName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public synchronized void setIsExistToFalse() {
if (isExist)
isExist &#61; false;
}
}