Apache Commons IO NameFileComparator Apache Commons IO FileAlterationMonitor Apache Commons IO SizeFileComparator 比较两个文件的名称。NameFileComparator可用于以区分大小写,不区分大小写或依赖于系统的区分大小写的方式使用其名称对文件列表或数组进行排序。 类声明 以下是 org.apache.commons.io.comparator.NameFileComparator 类的声明- public class NameFileComparator extends Object implements Serializable NameFileComparator类的示例 这是我们需要解析的输入文件 - Welcome to CodingDict. Simply Easy Learning. IOTester.java import java.io.File; import java.io.FileFilter; import java.io.IOException; import org.apache.commons.io.IOCase; import org.apache.commons.io.comparator.NameFileComparator; import org.apache.commons.io.filefilter.FileFileFilter; public class IOTester { public static void main(String[] args) { try { usingNameFileComparator(); } catch(IOException e) { System.out.println(e.getMessage()); } } public static void usingNameFileComparator() throws IOException { //get the current directory File currentDirectory = new File("."); NameFileComparator comparator = new NameFileComparator(IOCase.INSENSITIVE); File[] sortedFiles = comparator.sort(currentDirectory.listFiles((FileFilter)FileFileFilter.FILE)); System.out.println("Sorted By Name: "); for(File file:sortedFiles) { System.out.println(file.getName()); } } } 输出 它将打印以下结果。 Sorted By Name: .classpath .project input.txt Apache Commons IO FileAlterationMonitor Apache Commons IO SizeFileComparator