Java Path File Extention nio fileRenamer(final String fromPath, final String toPath, @Nullable final String toExtension, final boolean junkPaths)

Here you can find the source of fileRenamer(final String fromPath, final String toPath, @Nullable final String toExtension, final boolean junkPaths)

Description

file Renamer

License

Apache License

Declaration

public static Function<String, String> fileRenamer(final String fromPath, final String toPath,
            @Nullable final String toExtension, final boolean junkPaths) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import javax.annotation.Nullable;

import java.nio.file.*;

import java.util.function.Function;

public class Main {
    public static Function<String, String> fileRenamer(final String fromPath, final String toPath,
            @Nullable final String toExtension, final boolean junkPaths) {

        return new Function<String, String>() {

            @Override//from  ww w . ja  v  a2  s. c  om
            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());
                }
                if (toExtension != null) {
                    while (true) {
                        final int index = name.lastIndexOf('.');
                        if (index < 0 || name.length() - index > 4) {
                            break;
                        }
                        name = name.substring(0, index);
                    }
                    name = name + (toExtension.startsWith(".") ? "" : ".") + toExtension;
                }
                return toPath + name;
            }

        };
    }
}

Related

  1. asPaths(final Iterable files)
  2. changeExtension(Path source, String extension)
  3. changeFileExtension(Path fileName, String extension)
  4. fileExtension(final Path thePath)
  5. findFilesWithExtension(Path directory, String targetExtension)
  6. getAllPaths(Path root, final String... extensions)
  7. getExtension(final Path path)
  8. getExtension(Path file)