Java Utililty Methods Path File Extention nio

List of utility methods to do Path File Extention nio

Description

The list of methods to do Path File Extention nio are organized into topic(s).

Method

IterableasPaths(final Iterable files)
as Paths
return () -> new Iterator<Path>() {
    Iterator<? extends File> iter = files.iterator();
    @Override
    public boolean hasNext() {
        return iter.hasNext();
    @Override
    public Path next() {
...
PathchangeExtension(Path source, String extension)
change Extension
String newFileName = changeExtension(extension, source.getFileName().toString());
return source.getParent().resolve(newFileName);
PathchangeFileExtension(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));
StringfileExtension(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());
...
FunctionfileRenamer(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());
...
ListfindFilesWithExtension(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 {
...
ListgetAllPaths(Path root, final String... extensions)
get All Paths
List<Path> l = new ArrayList<>();
getAllPaths(l, root, extensions);
return l;
StringgetExtension(final Path path)
get Extension
int i = path.toString().lastIndexOf(".");
if (i >= 0) {
    return path.toString().substring(i + 1);
return "";
StringgetExtension(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];
StringgetExtension(Path path)
get Extension
return getExtension(path != null ? path.toString() : null);