下面列出了怎么用org.apache.commons.io.comparator.PathFileComparator的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Generates a file at .openapi-generator/FILES to track the files created by the user's latest run.
* This is ideal for CI and regeneration of code without stale/unused files from older generations.
*
* @param files The list tracking generated files
*/
private void generateFilesMetadata(List<File> files) {
if (generateMetadata) {
try {
StringBuilder sb = new StringBuilder();
File outDir = new File(this.config.getOutputDir());
List<File> filesToSort = new ArrayList<>();
// Avoid side-effecting sort in this path when generateMetadata=true
files.forEach(f -> {
// We have seen NPE on CI for getPath() returning null, so guard against this (to be fixed in 5.0 template management refactor)
//noinspection ConstantConditions
if (f != null && f.getPath() != null) {
filesToSort.add(f);
}
});
filesToSort.sort(PathFileComparator.PATH_COMPARATOR);
filesToSort.forEach(f -> {
String relativePath = outDir.toPath().relativize(f.toPath()).toString();
if (!relativePath.equals(METADATA_DIR + File.separator + "VERSION")) {
sb.append(relativePath).append(System.lineSeparator());
}
});
String targetFile = config.outputFolder() + File.separator + METADATA_DIR + File.separator + "FILES";
File filesFile = this.templateProcessor.writeToFile(targetFile, sb.toString().getBytes(StandardCharsets.UTF_8));
if (filesFile != null) {
files.add(filesFile);
}
} catch (Exception e) {
LOGGER.warn("Failed to write FILES metadata to track generated files.");
}
}
}
@Test
public void whenSortDirWithPathFileComparator_thenFirstFileaaatxt() throws IOException {
PathFileComparator pathFileComparator = new PathFileComparator(IOCase.INSENSITIVE);
String path = FilenameUtils.getFullPath(getClass().getClassLoader().getResource("fileTest.txt").getPath());
File dir = new File(path);
File[] files = dir.listFiles();
pathFileComparator.sort(files);
Assert.assertEquals("aaa.txt", files[0].getName());
}
/**
* Compares to file paths to see if they are the same based on the OS that is being used
*
* @param path1
* @param path2
* @return
*/
public static boolean areFilePathsSame(final String path1, final String path2) {
return PathFileComparator.PATH_SYSTEM_COMPARATOR.compare(new File(path1), new File(path2)) == 0 ? true : false;
}