Java implements automatic copying of specified content of a USB flash disk

Source: Internet
Author: User

The function of the program is to check the USB flash drive and automatically copy the content of the USB flash drive to a drive letter of the system. Share it with you as a small exercise for practicing io streams.

The implementation of this applet is as follows:
1. Check whether the system drive letter is added every other time after the program runs. You can use File. listRoots () to obtain the drive letter of the system.
2. If the drive letter is added, traverse the newly added drive letter and use the word to throttle the file to the specified path.

Note that the content of the USB flash drive may be large, so it is best to specify the file type to be copied, such as ppt, doc, and txt.
The following is the code for this applet:
You can specify the file type to be copied in the CopyThread class. You can add the corresponding file extension name to the fileTypes array. If you want to copy all files, set it to null. You can specify the storage path in the CopyFileToSysRoot class. Of course, if you want to, you can upload the file to the network disk, mailbox, and so on.

I. USB main class, program entry:

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 (); // enable the drive letter check thread new CheckRootThread (). start () ;}// interface private void launchFrame () {final JFrame frame = new JFrame (); frame. setdefaclocloseoperation (JFrame. EXIT_ON_CLOSE); frame. setLocation (450,250); JButton hide = new JButton ("click to hide window"); // click the button to hide window event listening hide. addActionListener (new ActionListener () {public void actionreceivmed (ActionEvent e) {frame. setVisible (false) ;}}); frame. add (hide); frame. pack (); frame. setVisible (true );}}

Ii. CheckRootThread class. This class is used to check the appearance of a new drive letter and trigger the copy of the new drive letter file.

Import java. io. file; // This class is used to check the appearance of the new drive letter and trigger the copy of the new drive letter File public class CheckRootThread extends Thread {// obtain the system drive letter private File [] sysRoot = File. listRoots (); public void run () {File [] currentRoot = null; while (true) {// current system drive letter 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 --) {// when the two drive letters are different, the copy if (currentRoot [I] of the new drive letter file is triggered. equals (sysRoot [j]) {isNewRoot = false ;}} if (isNewRoot) {new CopyThread (currentRoot [I]). start () ;}} sysRoot = File. listRoots (); // check the system drive letter every 5 seconds try {Thread. sleep (5000);} catch (InterruptedException e) {e. printStackTrace ();}}}}

Iii. CopyThread class for file traversal and selecting the specified file format for copying:

Import java. io. File;

// This class is used to copy the new drive letter file
Public class CopyThread extends Thread {
// Set the file type to be copied. If you want to copy all files in the format, set fileTypes to 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 );
}

// Traverse the drive letter file and copy the 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 ();
}
}
}

// Match the file type to be copied
Public boolean fileTypeMatch (File f ){
// If fileTypes is null, all copies are performed.
If (fileTypes = null ){
Return true;
} Else {
For (String type: fileTypes ){
If (f. getName (). endsWith ("." + type )){
Return true;
}
}
}
Return false;
}
}
 

4. CopyFileToSysRoot class:

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; // copy IOpublic class CopyFileToSysRoot {// copy the File storage PATH private static final String PATH = "D: USB"; private file File = null; public CopyFileToSysRoot (file File file) {this. file = file;} // copy the file pu Blic void doCopy () {BufferedInputStream bis = null; BufferedOutputStream bos = null; try {// create directory 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 () ;}}// create the 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 ();}}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.