Java之——U盘检测程序&文件递归

转载请注明出处: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 Vector<File> fileVector = new Vector<File>();
	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 Vector<File> fileVector = null;

	private int scanNum = 1;

	/** 线程安全的变量,用于退出线程 */
	volatile boolean isExist = true;

	public SearchFileThread(Vector<File> fileVector) {
		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 <= fileVector.size()) {

				try {
					System.out.println("search:"
							+ fileVector.elementAt(scanNum - 1).toString()
							+ " Total Space:"
							+ fileVector.elementAt(scanNum - 1).getTotalSpace()
							/ 1024 / 1024 + "MB Free Space:"
							+ fileVector.elementAt(scanNum - 1).getFreeSpace()
							/ 1024 / 1024 + "MB");
					/** 遍历文件内容 */
					getFiles(fileVector.elementAt(scanNum - 1).getPath());
					scanNum++;
				} catch (Exception e) {
					e.printStackTrace();
					scanNum++;
				}
			}
			/** 如果盘符的大小发生变化,则有文件进出 */
			if (totalMemory != file.getFreeSpace()) {
				System.out.println("文件发生变化----------------------");
				getFiles(file.getPath());
				totalMemory = file.getFreeSpace();
			}
		}

	}

	/**
	 * 递归遍历文件
	 * @param path
	 */
	public void getFiles(String path) {
		try {
			File file = new File(path);
			if (file.isDirectory()) {
				File[] list = file.listFiles();
				for (int i = 0; i < list.length; i++) {
					if (list[i].isDirectory()) {
						/** 递归调用 */
						getFiles(list[i].getPath());
					}
					System.out.println("Find File:" + list[i].getName());
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public synchronized void setIsExistToFalse() {
		if (isExist)
			isExist = false;
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

冰 河

可以吃鸡腿么?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值