JAVA代码实现得到指定文件夹下的文件名

//代码片段小解析~~
File f = new File(path);
File[] t = f.listFiles(); //这个方法来读取这个路径下的文件


import
java.io.PrintWriter; import java.io.FileReader; import java.io.File; class GetAllFileName{ public static void process(String path,String to){ File f=new File(path); File list[] = f.listFiles(); StringBuffer sb=new StringBuffer(); for(int i=0;i<list.length;i++){ if(list[i].isFile()){ String temp = list[i].getName(); sb.append(temp); sb.append('\n'); } } try{ PrintWriter pw=new PrintWriter(to); pw.write(sb.toString()); pw.close(); } catch(Exception e){ e.printStackTrace(); } } public static void main(String []args) String from="titles"; String to="titles.txt"; process(from,to); } }

 

posted @ 2018-03-21 12:45  陈晨晨00  阅读(358)  评论(0编辑  收藏  举报