Java Utililty Methods Path File Name nio

List of utility methods to do Path File Name nio

Description

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

Method

StringgetFileName(Path path)
get File Name
return path.getFileName().toString();
StringgetFileName(String fullPath, boolean withParentFolder)
get File Name
Path p = Paths.get(fullPath);
String file = p.getFileName().toString();
if (withParentFolder)
    file = p.getParent().getFileName() + "/" + file;
return file;
StringgetFileName(String path)
get File Name
Path p = Paths.get(path);
String fname = p.getFileName().toString();
int lio = fname.lastIndexOf(".");
return fname.substring(0, lio);
StringgetFileName(String relativePath)
get File Name
Path path = Paths.get(relativePath);
return path.getFileName().toFile().getName();
StringgetFileNameFromPath(String filePath)
get File Name From Path
Path p = Paths.get(filePath);
return p.getFileName().toString();
StringgetFileNameFromPath(String str)
parse the file name from file path string
if (str == null || str.equals("") || str.isEmpty()) {
    return null;
Path p = Paths.get(str);
String fileName = p.getFileName().toString();
return fileName;
StringgetFilenameOrLastPath(String path)
Parses a full path, returning the filename.
Path p = Paths.get(path);
return p.getFileName().toString();
ListgetFileNames(List fileNames, Path dir)
get File Names
int foundFiles = 0;
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
    for (Path path : stream) {
        if (path.toFile().isDirectory()) {
            getFileNames(fileNames, path);
        } else {
            foundFiles++;
            fileNames.add(path.toAbsolutePath().toString());
...
ListgetFileNames(List fileNames, Path dir, String ext, boolean recursive)
Gets all files in target directory, with the given file extension.
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
    stream.forEach(p -> {
        if (p.toFile().isDirectory() && recursive) { 
            getFileNames(fileNames, p, ext, true);
        } else { 
            String fileName = p.toAbsolutePath().toString();
            if (fileName.endsWith(ext))
                fileNames.add(fileName);
...
voidgetFileNames(List fileNames, Path dir, String sFilter)
get File Names
if (!dir.toFile().isDirectory()) {
    fileNames.add(dir.toString());
    return;
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, sFilter)) {
    for (Path path : stream) {
        fileNames.add(path.toAbsolutePath().toString());
} catch (IOException e) {
    e.printStackTrace();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
    for (Path path : stream) {
        if (path.toFile().isDirectory()) {
            getFileNames(fileNames, path, sFilter);
} catch (IOException e) {
    e.printStackTrace();