Commons Io 简明教程
Apache Commons IO - NameFileComparator
NameFileComparator 比较两个文件的名称。它可用于使用文件名以区分大小写、不区分大小写或系统相关的区分大小写方式对文件列表或数组进行排序。
NameFileComparator compare the names of two files. It can be used to sort the lists or arrays of files, using their name, either in a case-sensitive, case-insensitive or system dependent case sensitive way.
Class Declaration
以下是声明:
Following is the declaration for
org.apache.commons.io.comparator.NameFileComparator 类:
org.apache.commons.io.comparator.NameFileComparator Class −
public class NameFileComparator
extends Object implements Serializable
Example of NameFileComparator Class
以下是我们需要解析的输入文件 -
Here is the input file we need to parse −
Welcome to TutorialsPoint. 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());
}
}
}