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;//文件复制IOpublic 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();}}

观今宜鉴古,无古不成今。

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

相关文章:

你感兴趣的文章:

标签云: