返回文章页面如何在字母顺序中建立文件列表?

我的代码如下:

class ListPageXMLFiles implements FileFilter {


@Override
public boolean accept(File pathname) {
DebugLog.i("ListPageXMLFiles", "pathname is " + pathname);


String regex = ".*page_\\d{2}\\.xml";
if(pathname.getAbsolutePath().matches(regex)) {
return true;
}
return false;
}
}


public void loadPageTrees(String xml_dir_path) {
ListPageXMLFiles filter_xml_files = new ListPageXMLFiles();
File XMLDirectory = new File(xml_dir_path);
for(File _xml_file : XMLDirectory.listFiles(filter_xml_files)) {
loadPageTree(_xml_file);
}
}

FileFilter运行得很好,但是 listFiles()似乎在反向列出这些文件的字母顺序。有没有什么快速的方法告诉 ABc2以字母顺序列出文件?

135124 次浏览

The listFiles method, with or without a filter does not guarantee any order.

It does, however, return an array, which you can sort with Arrays.sort().

File[] files = XMLDirectory.listFiles(filter_xml_files);
Arrays.sort(files);
for(File _xml_file : files) {
...
}

This works because File is a comparable class, which by default sorts pathnames lexicographically. If you want to sort them differently, you can define your own comparator.

If you prefer using Streams:

A more modern approach is the following. To print the names of all files in a given directory, in alphabetical order, do:

Files.list(Paths.get(dirName)).sorted().forEach(System.out::println)

Replace the System.out::println with whatever you want to do with the file names. If you want only filenames that end with "xml" just do:

Files.list(Paths.get(dirName))
.filter(s -> s.toString().endsWith(".xml"))
.sorted()
.forEach(System.out::println)

Again, replace the printing with whichever processing operation you would like.

I think the previous answer is the best way to do it here is another simple way. just to print the sorted results.

 String path="/tmp";
String[] dirListing = null;
File dir = new File(path);
dirListing = dir.list();
Arrays.sort(dirListing);
System.out.println(Arrays.deepToString(dirListing));

This is my code:

        try {
String folderPath = "../" + filePath.trim() + "/";
logger.info("Path: " + folderPath);
File folder = new File(folderPath);
File[] listOfFiles = folder.listFiles();
int length = listOfFiles.length;
logger.info("So luong files: " + length);
ArrayList<CdrFileBO> lstFile = new ArrayList< CdrFileBO>();


if (listOfFiles != null && length > 0) {
int count = 0;
for (int i = 0; i < length; i++) {
if (listOfFiles[i].isFile()) {
lstFile.add(new CdrFileBO(listOfFiles[i]));
}
}
Collections.sort(lstFile);
for (CdrFileBO bo : lstFile) {
//String newName = START_NAME + "_" + getSeq(SEQ_START) + "_" + DateSTR + ".s";
String newName = START_NAME + DateSTR + getSeq(SEQ_START) + ".DAT";
SEQ_START = SEQ_START + 1;
bo.getFile().renameTo(new File(folderPath + newName));
logger.info("newName: " + newName);
logger.info("Next file: " + getSeq(SEQ_START));
}


}
} catch (Exception ex) {
logger.error(ex);
ex.printStackTrace();
}

In Java 8:

Arrays.sort(files, (a, b) -> a.getName().compareTo(b.getName()));

Reverse order:

Arrays.sort(files, (a, b) -> -a.getName().compareTo(b.getName()));