|  
 
 
java实现u盘指定内容的自动复制 
这个小程序的功能是,检查U盘,并将U盘的内容自动拷贝到系统的某个盘符中。分享给大家,就当作是练习io流的小练习。 
  
 这个小程序的实现方法如下: 
1、程序运行后隔一断时间就检查系统的盘符有没有增加,通过File.listRoots()可获取系统存在的盘符。 
2、如果盘符增加了,遍历这个新增加的盘符,用字节流拷贝文件到指定的路径。 
  
 需要注意的是,由于U盘的内容可能很大,所以拷贝的时候最好指定要拷贝的文件类型,如ppt,doc,txt等等。 
 下面是这个小程序的相关代码: 
在CopyThread类中可以指定要复制的文件类型,大家在fileTypes数组中加入相应的文件后缀名即可。如果要复制所有文件,将其设为null就行了。在CopyFileToSysRoot类中可以指定存储的路径,当然,如果愿意的话,你可以将文件上传到网盘,邮箱等等。。。 
  
 一、USBMain类,程序入口: 
 [java] view plaincopyprint? 01.import java.awt.event.ActionEvent;   02.import java.awt.event.ActionListener;   03.import javax.swing.JButton;   04.import javax.swing.JFrame;   05.   06.public class USBMain {   07.   08.    public static void main(String[] args) {   09.        USBMain u = new USBMain();   10.        u.launchFrame();   11.        //开启盘符检查线程    12.        new CheckRootThread().start();   13.    }   14.   15.    // 界面    16.    private void launchFrame() {   17.        final JFrame frame = new JFrame();   18.        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);   19.        frame.setLocation(450, 250);   20.        JButton hide = new JButton("点击隐藏窗口");   21.        // 点击按钮后隐藏窗口事件监听    22.        hide.addActionListener(new ActionListener() {   23.            public void actionPerformed(ActionEvent e) {   24.                frame.setVisible(false);   25.            }   26.        });   27.        frame.add(hide);   28.        frame.pack();   29.        frame.setVisible(true);   30.    }   31.}   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类,此类用于检查新盘符的出现,并触发新盘符文件的拷贝。  [java] view plaincopyprint? 01.import java.io.File;   02.   03.//此类用于检查新盘符的出现,并触发新盘符文件的拷贝    04.public class CheckRootThread extends Thread {   05.    // 获取系统盘符    06.    private File[] sysRoot = File.listRoots();   07.   08.    public void run() {   09.        File[] currentRoot = null;   10.        while (true) {   11.            // 当前的系统盘符    12.            currentRoot = File.listRoots();   13.            if (currentRoot.length > sysRoot.length) {   14.                for (int i = currentRoot.length - 1; i >= 0; i--) {   15.                    boolean isNewRoot = true;   16.                    for (int j = sysRoot.length - 1; j >= 0; j--) {   17.                        // 当两者盘符不同时,触发新盘符文件的拷贝    18.                        if (currentRoot[i].equals(sysRoot[j])) {   19.                            isNewRoot = false;   20.                        }   21.                    }   22.                    if (isNewRoot) {   23.                        new CopyThread(currentRoot[i]).start();   24.                    }   25.                }   26.            }   27.            sysRoot = File.listRoots();   28.            //每5秒时间检查一次系统盘符    29.            try {   30.                Thread.sleep(5000);   31.            } catch (InterruptedException e) {   32.                e.printStackTrace();   33.            }   34.        }   35.    }   36.}   import java.io.File; [1] [2] 下一页  
 |