背景:将程序目录下的指定文件拷贝到其他目录,我的拷贝可以成功,但是为什么1个文件拷贝了5次,伤脑筋,贴代码如下: public static void main(String[] args) throws Exception {
// 首先创建文件过滤类
FileFilter fl = new FileFilter() { @Override
public boolean accept(File pathname) { String s = pathname.getName().toLowerCase();
if (s.startsWith("abc")) {
return true;
}
return false;
} };
File f = new File(".");
// 第二部根据过滤器过滤指定目录下的文件、
getFile(fl, f); } /**
* 递归调用查找文件
*
* @param fl
* @param src
* @throws FileNotFoundException
* @throws IOException
*/
private static void getFile(FileFilter fl, File src)
throws FileNotFoundException, IOException { File[] outFile = src.listFiles();
for (File out : outFile) {
if (out.isDirectory()) {
getFile(fl, out);
} else {
File[] inner = src.listFiles(fl);
for (File file : inner) {
InputStream in = new FileInputStream(file);
byte[] bt = new byte[(int) file.length()];
in.read(bt);
OutputStream outs = new FileOutputStream("D:/"
+ new Date().getTime() + "." + file.getName());
outs.write(bt);
} } }
}
// 首先创建文件过滤类
FileFilter fl = new FileFilter() { @Override
public boolean accept(File pathname) { String s = pathname.getName().toLowerCase();
if (s.startsWith("abc")) {
return true;
}
return false;
} };
File f = new File(".");
// 第二部根据过滤器过滤指定目录下的文件、
getFile(fl, f); } /**
* 递归调用查找文件
*
* @param fl
* @param src
* @throws FileNotFoundException
* @throws IOException
*/
private static void getFile(FileFilter fl, File src)
throws FileNotFoundException, IOException { File[] outFile = src.listFiles();
for (File out : outFile) {
if (out.isDirectory()) {
getFile(fl, out);
} else {
File[] inner = src.listFiles(fl);
for (File file : inner) {
InputStream in = new FileInputStream(file);
byte[] bt = new byte[(int) file.length()];
in.read(bt);
OutputStream outs = new FileOutputStream("D:/"
+ new Date().getTime() + "." + file.getName());
outs.write(bt);
} } }
}
* 递归调用查找文件
*
* @param fl
* @param src
* @throws FileNotFoundException
* @throws IOException
*/
private static void getFile(FileFilter fl, File src)
throws FileNotFoundException, IOException {
File[] outFile = src.listFiles();
for (File out : outFile) {
if (out.isDirectory()) {
getFile(fl, out);
} else {
File[] inner = src.listFiles(fl);
for (File file : inner) {
InputStream in = new FileInputStream(file);
byte[] bt = new byte[(int) file.length()];
in.read(bt);
OutputStream outs = new FileOutputStream("D:/"
+ new Date().getTime() + "." + file.getName());
outs.write(bt);
}
}
}
}
这里的递归方法的问题,第一次for循环查找出来的文件数量,已经决定了你复制文件的次数了,建议写递归方法返回数据,然后对返回的文件进行复制。
/**
* 递归调用查找文件
*
* @param fl
* @param src
* @throws FileNotFoundException
* @throws IOException
*/
private static List<File> getFile(File src)
throws FileNotFoundException, IOException {
List<File> files = new ArrayList<File>();
File[] outFile = src.listFiles();
for (File out : outFile) {
if (out.isDirectory()) {
files.addAll(getFile(out));
} else if(out.getName().toLowerCase().startWith("abc")){
files.add(out);
}
}
return files;
}private static void copy(List<File> files){
for(File f : files){
InputStream in = null;
OutputStream outs = null;
try{
in = new FileInputStream(file);
byte[] bt = new byte[(int) f.length()];
in.read(bt);
outs = new FileOutputStream("D:/"+ new Date().getTime() + "." + f.getName());
outs.write(bt);
}catch(IOException e){
e.printStackTrace();
}finally{
if(outs != null){
try{
outs.close();
}catch(IOException e){}
}
if(in != null){
try{
in.close();
}catch(IOException e){}
}
}
}
}
另外,写代码的时候 最好养成释放资源的习惯,要不然对程序性能影响很大。