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

StringinsertSuffixBeforeExtension(String path, String suffix)
insert Suffix Before Extension
return getFullNameWithoutExtension(path) + suffix + "." + getExtension(path);
ListlistFile(final String path, final String extension)
list File
if (!new File(path).isDirectory()) {
    throw new IllegalArgumentException("Path " + path + " is not a directory");
final List<String> files = new ArrayList<>();
Files.walk(Paths.get(path)).filter(Files::isRegularFile)
        .filter((p) -> p.toFile().getAbsolutePath().endsWith(extension))
        .forEach(p -> files.add(p.toString()));
return files;
...
ListlistFiles(final Path path, final String... extensions)
List all files with extension
return Files.list(path).filter(child -> {
    if (extensions.length > 0) {
        for (String extension : extensions) {
            if (child.getFileName().toString().endsWith(extension)) {
                return true;
        return false;
...
String[]listFiles(String path, String extension)
list Files
String[] paths = EMPTY_FILE_LIST;
if (path.endsWith("**")) {
    String dir = path.substring(0, path.length() - 2);
    if (Paths.get(dir).toFile().isDirectory()) {
        paths = listFiles(dir, extension, true);
    } else if (!Paths.get(dir).toFile().exists()) {
        throw new IOException("Directory " + dir + " does not exist");
} else if (path.endsWith("*")) {
    String dir = path.substring(0, path.length() - 1);
    if (Paths.get(dir).toFile().isDirectory()) {
        paths = listFiles(dir, extension, false);
    } else if (!Paths.get(dir).toFile().exists()) {
        throw new IOException("Directory " + dir + " does not exist");
} else {
    paths = getSingleFile(path, extension);
return paths;
StringremoveExtension(final Path path)
remove Extension
final Path fileName = path.getFileName();
if (fileName == null)
    throw new IllegalArgumentException();
final String n = fileName.toString();
final int i = n.lastIndexOf('.');
return i == -1 ? n : n.substring(0, i);
PathremoveExtension(Path file)
Remove the extension information from the path.
Predicate<Path> filter = path -> {
    try {
        return !Files.isDirectory(path) && !Files.isHidden(path);
    } catch (IOException e) {
        throw new RuntimeException(e);
};
Function<Path, Path> removeExtensionFunction = path -> {
...
StringreplaceExtensionWith(String toFilePath, String newExtension)
replace Extension With
String dirname = toFilePath.substring(0, toFilePath.lastIndexOf(File.separatorChar) + 1);
String filename = toFilePath.substring(toFilePath.lastIndexOf(File.separatorChar) + 1, toFilePath.length());
String basename = filename.contains(".") ? filename.substring(0, filename.lastIndexOf('.') + 1)
        : (filename + ".");
return Paths.get(dirname, basename + newExtension).toString();
StringstripExtension(Path path, String extension)
strip Extension
String fileName = path.toString();
return fileName.substring(0, fileName.toLowerCase().indexOf(extension));