关于List集合中又有list集合的问题

可爱难缠 2018-06-04 11:00:35
我要做的事情是 遍历一个盘的所有文件信息(如果是文件夹要提取它下面的文件信息)
但是我遍历list集合的时候他只有两个路径,之前的路径是被覆盖了吗?为什么覆盖啊 谢谢各位小哥哥解答

public void work(){
//定义一个数组存放路径
List<String> list = new ArrayList<String>();

//创建一个File对象
File file =new File("Q:"+File.separator);
File [] fe = file.listFiles();
//判断是目录还是文件
if(fe!=null){
for (File st : fe) {

if(st.isDirectory()){
//是目录继续执行
if(digui(st)!=null){
list.addAll(digui(st));
}
}else if(st.isFile()){
//是标准文件
System.out.println("标准文件:");
list.add(st.getPath()) ;
break;

}else if(st.isHidden()){
//隐藏文件
System.out.println("隐藏文件:");
list.add( st.getPath());
break;

}



}
}

//查看集合

for (String name1 : list) {
System.out.println("路径名"+name1);
}


}

public List<String> digui(File fe){
List<String> name = new ArrayList<String>();
//传进来一个目录,查找目录下方文件
File[] fw =fe.listFiles();
if(fw!=null){
for (File le : fw) {
if(le.isDirectory()){
//是目录
digui(le);
}else if(le.isFile()){
//是普通文件
System.out.println("普通文件:"+le.getPath());
name.add(le.getPath());
return name;
}else if(le.isHidden()){
//隐秘文件
name.add(le.getPath());
return name;
}
}
}

return null;
}



...全文
3782 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
nayi_224 2018-06-05
  • 打赏
  • 举报
回复
楼上的,给新手看这种东西,你的良心不会痛吗??? 楼主还是用这个吧,一共就7行的递归 import java.io.File; import java.util.ArrayList; import java.util.List; public class Test { public static void main(String[] args) { File file = new File("d://tomcat8081"); if(!file.isDirectory()){ System.out.println(file.getPath()); return; } List<String> list = work(file); for(String path : list) System.out.println(path); } public static List<String> work(File file){ List<String> list = new ArrayList(); for(File fff : file.listFiles()) if(fff.isDirectory()) list.addAll(work(fff)); else list.add(fff.getPath()); return list; } }
可爱难缠 2018-06-04
  • 打赏
  • 举报
回复
好像是break的问题 弄了我好久 我真是蠢 - -
  • 打赏
  • 举报
回复
你这里用break 不对吧。https://blog.csdn.net/u010597493/article/details/54311324 找到一个非目录的文件就跳出循环了。这里用 continue比较合适吧!
attilax 2018-06-04
  • 打赏
  • 举报
回复
送你一个我用的遍历文件夹发算法 package com.attilax.io; import java.io.File; import java.io.IOException; import java.util.List; import java.util.Map; import org.apache.commons.io.FileUtils; import org.apache.log4j.Logger; import com.attilax.collection.mapBuilder; import com.attilax.core.BreakException; import com.attilax.core.ForeachFunction; import com.attilax.util.ExUtil; import com.attilax.util.timestampUtil; import com.cnhis.cloudhealth.module.log.Cls1; import com.google.common.base.Function; import com.google.common.collect.Lists; import com.google.common.collect.Maps; public class DirTraveService { public static Logger logger = Logger.getLogger(Cls1.class); public static void main(String[] args) { DirTraveService dts=new DirTraveService(); final String dir = "C:\\Users\\attilax\\Documents\\s2018 s4 doc compc dtS44"; dts.traveV4Vs427(dir, new Function<File, Object>() { @Override public Object apply(File arg0) { logger.info("arg0:"+arg0.getAbsolutePath()); String reltpath=PathService.relpath(arg0.getAbsolutePath(),dir); String newFileTxtmode4index="c:\\00txtmodeIndexdir\\"+reltpath+".txt"; try { String txtcontext = "data"+timestampUtil.gettimeStamp_millsec(); logger.info("txtcontext:"+txtcontext); FileUtils.write(new File(newFileTxtmode4index), txtcontext); } catch (IOException e) { e.printStackTrace(); logger.error(e); } return null; } }, new Function<Integer, Object>() { @Override public Object apply(Integer cnt_index) { // Map m=Maps.newConcurrentMap();m.put("index", cnt_index) logger.info("cnt_index:"+cnt_index); return null; }}); } /** * ingo ex ,and log ,and save ex in to li.. * * * * * @param strPath * @param fun */ public void traveV5_vS522( File dir , ForeachFunction fun ) { // File dir = new File(strPath); // Function<T, R> File[] files = dir.listFiles(); if (files == null) return; int length = files.length; for (int i = 0; i < length; i++) { if (files[i].isDirectory()) { traveV5_vS522(files[i] ,fun); } else { File f = files[i]; try { count++; fun.each(count,f); }catch( BreakException e) { Map dbginfo=mapBuilder.$().put("index", count).put("file", f).build(); ExUtil.throwExV4("fun.each ex", e,dbginfo); } catch (Exception e) { Map m=Maps.newConcurrentMap(); m.put("file", f); m.put("ex", e); traveExs.add(m); } // logger.info("--now process count:"+String.valueOf(count)); } } } public List<Map> traveExs=Lists.newArrayList(); int count=0; /** * ingo ex ,and log ,and save ex in to li.. * * * * * @param strPath * @param fun */ public void traveV4Vs427(String strPath, Function fun,Function fun_log) { File dir = new File(strPath); // Function<T, R> File[] files = dir.listFiles(); if (files == null) return; int length = files.length; for (int i = 0; i < length; i++) { if (files[i].isDirectory()) { traveV4Vs427(files[i].getAbsolutePath(),fun,fun_log); } else { File strFileName = files[i]; try { fun.apply(strFileName); } catch (Exception e) { Map m=Maps.newConcurrentMap(); m.put("file", strFileName); m.put("ex", e); } count++; fun_log.apply(count); // logger.info("--now process count:"+String.valueOf(count)); } } } /** * ingo ex ,and log ,and save ex in to li.. * * * * * @param strPath * @param fun */ public void trave_throwEx(String strPath, Function fun,Function fun_log) { File dir = new File(strPath); // Function<T, R> File[] files = dir.listFiles(); if (files == null) return; int length = files.length; for (int i = 0; i < length; i++) { if (files[i].isDirectory()) { trave_throwEx(files[i].getAbsolutePath(),fun,fun_log); } else { File strFileName = files[i]; fun.apply(strFileName); count++; fun_log.apply(count); // logger.info("--now process count:"+String.valueOf(count)); } } } public static Function nullFunc() { Function fc= new Function<Integer, Object>() { @Override public Object apply(Integer cnt_index) { // Map m=Maps.newConcurrentMap();m.put("index", cnt_index) // logger.info("cnt_index:"+cnt_index); return null; }}; return fc; } }
nayi_224 2018-06-04
  • 打赏
  • 举报
回复
不只是break的问题。改成continue你也只能获取一级文件以及一级文件夹下的第一个文件。

51,411

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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