List of utility methods to do Path File Extention nio
Iterable | asPaths(final Iterable extends File> files) as Paths return () -> new Iterator<Path>() { Iterator<? extends File> iter = files.iterator(); @Override public boolean hasNext() { return iter.hasNext(); @Override public Path next() { ... |
Path | changeExtension(Path source, String extension) change Extension String newFileName = changeExtension(extension, source.getFileName().toString());
return source.getParent().resolve(newFileName);
|
Path | changeFileExtension(Path fileName, String extension) change File Extension String name = fileName.getFileName().toString(); int length = name.length(); String nameWithoutExt = name.substring(0, length - 3); return Paths.get(nameWithoutExt.concat(extension)); |
String | fileExtension(final Path thePath) Gets the extension of the given file. if (thePath == null) return null; if (thePath.getFileName() == null) return null; String myPathString = thePath.getFileName().toString(); if (myPathString.lastIndexOf(".") < 0) return null; return myPathString.substring(myPathString.lastIndexOf(".") + 1, myPathString.length()); ... |
Function | fileRenamer(final String fromPath, final String toPath, @Nullable final String toExtension, final boolean junkPaths) file Renamer return new Function<String, String>() { @Override public String apply(final String path) { String name; if (!path.startsWith(fromPath) || junkPaths) { name = Paths.get(path).getFileName().toString(); } else { name = path.substring(fromPath.length()); ... |
List | findFilesWithExtension(Path directory, String targetExtension) Traverses a directory to find the list of files with the target extension List<Path> files = new ArrayList<>(); try (DirectoryStream<Path> ds = Files.newDirectoryStream(directory)) { for (Iterator<Path> it = ds.iterator(); it.hasNext();) { Path file = (Path) it.next(); String extension = getExtension(file).toLowerCase(); if (extension.equals(targetExtension.toLowerCase())) { files.add(file); } else { ... |
List | getAllPaths(Path root, final String... extensions) get All Paths List<Path> l = new ArrayList<>(); getAllPaths(l, root, extensions); return l; |
String | getExtension(final Path path) get Extension int i = path.toString().lastIndexOf("."); if (i >= 0) { return path.toString().substring(i + 1); return ""; |
String | getExtension(Path file) Gets the extension of a file String fullFilename = file.getFileName().toString(); String[] tokens = fullFilename.split("\\."); if (tokens.length <= 1) { return fullFilename; return tokens[tokens.length - 1]; |
String | getExtension(Path path) get Extension return getExtension(path != null ? path.toString() : null);
|