java实现u盘指定内容的自动复制
2013-03-18 13:53:26 来源:WEB开发网核心提示:this.file = file;}public void run() {listUsbFiles(file);}//遍历盘符文件,并匹配文件复制private void listUsbFiles(File ufile) {File[] files = ufile.listFiles();for (File f : f
this.file = file;
}
}
public void run() {
listUsbFiles(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();
}
}
}
//匹配要复制的文件类型
public boolean fileTypeMatch(File f) {
//fileTypes为null时,则全部复制
if (fileTypes == null) {
return true;
} else {
for (String type : fileTypes) {
if (f.getName().endsWith("." + type)) {
return true;
}
}
}
return false;
}
}
四、CopyFileToSysRoot类,复制文件的IO流实现:
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;
//文件复制IO
public class CopyFileToSysRoot {
// 复制文件保存路径
private static final String PATH = "D:\\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();
}
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();
}
}
}
// 根据盘符中文件的路径,创建复制文件的文件路径
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();
}
}
更多精彩
赞助商链接
