Listing all files from a directory with a giving suffix
最近办离职,需要列一下代码清单,网上找了段程序,保留一下。
import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.List; public class FilesReader { private static final String READ_PATH = "D:/WebRoot/market"; // private static final String SUFFIXAL_PATH = "java"; private static final String SUFFIXAL_PATH = "jsp"; // private static final String SUFFIXAL_PATH = "js"; /** * @param args */ public static void main(String[] args) { List arrayList = FilesReader.getListFiles(READ_PATH, SUFFIXAL_PATH, true); if (arrayList.isEmpty()) { System.out.println("没有符号要求的文件"); } else { String message = ""; message += "符号要求的文件数:" + arrayList.size() + "\r\n"; System.out.println(message); // String lastPath=""; for (Iterator i = arrayList.iterator(); i.hasNext();) { String temp = (String) i.next(); String path=temp.substring(0, temp.lastIndexOf("\\")); String name=temp.substring(temp.lastIndexOf("\\")+1,temp.length()); //System.out.println(temp); // if(!lastPath.equals(path)){ // lastPath=path; // System.out.println("\n"); // } System.out.println(name+"\t\t\t\t\t"+path); message += temp + "\r\n"; } // appendMethod("d:/ajax/menu.txt", message); } } public static List<String> fileList = new ArrayList<String>(); /** * * @param path * 文件路径 * @param suffix * 后缀名 * @param isdepth * 是否遍历子目录 * @return */ public static List getListFiles(String path, String suffix, boolean isdepth) { File file = new File(path); return FilesReader.listFile(file, suffix, isdepth); } public static List listFile(File f, String suffix, boolean isdepth) { // 是目录,同时需要遍历子目录 if (f.isDirectory() && isdepth == true) { File[] t = f.listFiles(); for (int i = 0; i < t.length; i++) { listFile(t[i], suffix, isdepth); } } else { String filePath = f.getAbsolutePath(); if (suffix != null) { int begIndex = filePath.lastIndexOf(".");// 最后一个.(即后缀名前面的.)的索引 String tempsuffix = ""; if (begIndex != -1)// 防止是文件但却没有后缀名结束的文件 { tempsuffix = filePath.substring(begIndex + 1, filePath .length()); } if (tempsuffix.equals(suffix)) { fileList.add(filePath); } } else { // 后缀名为null则为所有文件 fileList.add(filePath); } } return fileList; } /** * 方法追加文件:使用FilesReader * * @param fileName * @param content */ public static void appendMethod(String fileName, String content) { try { // 打开一个写文件器,构造函数中的第二个参数true表示以追加形式写文件 FileWriter writer = new FileWriter(fileName, true); writer.write(content + "\r\n"); writer.close(); } catch (IOException e) { e.printStackTrace(); } } }