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

PathgetDataPath(String filename)
get Data Path
return Paths.get("data", filename);
StringgetDeckNameFromFile(final Path deckFile)
Gets the name of a deck file without the extension.
return getDeckNameFromFilename(deckFile.getFileName().toString());
byte[]getDefaultPathName(final String address, final long lspId)
get Default Path Name
return ("pcc_" + address + "_tunnel_" + lspId).getBytes(StandardCharsets.UTF_8);
SetgetDirectoryNames(Path baseDirectory)
get Directory Names
LinkedHashSet<String> result = new LinkedHashSet<>();
try {
    for (Path p : Files.newDirectoryStream(baseDirectory)) {
        if (Files.isDirectory(p)) {
            String name = p.getFileName().toString();
            if (!result.add(name)) {
                throw new RuntimeException("duplicate directory: " + name);
} catch (IOException e) {
    throw new RuntimeException("Error while reading directories", e);
return result;
StringgetFileContent(String fileNameOrPath)
get File Content
String result = "";
try {
    BufferedReader br = new BufferedReader(
            new InputStreamReader(new DataInputStream(new FileInputStream(fileNameOrPath))));
    String line;
    while ((line = br.readLine()) != null)
        result += line + "\n";
    br.close();
...
FilegetFileInFolder(Path p, String filename)
get File In Folder
Path path = Paths.get(p.toString(), filename);
File f = new File(path.toString());
f.createNewFile();
return f;
StringgetFilename(final String filepath)
get Filename
final Path p = Paths.get(filepath);
final Path filename = p.getFileName();
return filename == null ? null : filename.toString();
StringgetFilename(final Path path)
Needed to get rid of findbugs "dodgy code warnings" in regards to getting the filename of a path as a string
String filename = "";
if (path != null) {
    final Path filenamePath = path.getFileName();
    if (filenamePath != null) {
        filename = filenamePath.toString();
return filename;
...
StringgetFilename(final Path path)
Return the raw filename of the specified path.
Strip all the parent folders and the extension of the file to just return the filename.
Objects.requireNonNull(path);
if (Files.isDirectory(path)) {
    throw new IllegalArgumentException("Cannot get name of a directory");
final String filename = path.getFileName().toString();
final int extensionSeparatorIndex = filename.lastIndexOf(FILE_EXTENSION_SEPARATOR);
if (extensionSeparatorIndex <= 0 || extensionSeparatorIndex == filename.length() - 1) {
    return filename;
...
StringgetFileName(Path path)
Root path have no file name.
String pathString = null;
if (path != null) {
    if (path.getFileName() != null) {
        pathString = path.getFileName().toString();
    } else {
        pathString = path.toString();
return pathString;