一个恼人的IO问题

TheNextGates 2016-02-06 03:26:31
背景:将程序目录下的指定文件拷贝到其他目录,我的拷贝可以成功,但是为什么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);
}

}

}
}
...全文
332 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
没睡醒的眼睛 2016-02-16
  • 打赏
  • 举报
回复
/** * 递归调用查找文件 * * @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){} } } } } 另外,写代码的时候 最好养成释放资源的习惯,要不然对程序性能影响很大。
king_1993 2016-02-15
  • 打赏
  • 举报
回复
你这个程序不管怎样,都会遍历当前文件的所在目录。 你可以把你要复制的文件放在另一个目录中。在另一个目录中放置6个文件,看是不是复制了6次。嘿嘿嘿
rickylin86 2016-02-06
  • 打赏
  • 举报
回复
看看你上面贴的代码的39行.已经list过一次了,然后判断out是一个文件而非目录.这时候应该执行的是拷贝工作,而不是重新list一次.
乘着风去破浪 2016-02-06
  • 打赏
  • 举报
回复
应该是第一次for循环的问题,你查询下循环多少次,

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧