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

StringgetNormalizedFileName(Path file)
get Normalized File Name
String normalizedFileName = file.toAbsolutePath().normalize().toString();
if (File.separatorChar != '/') {
    normalizedFileName = normalizedFileName.replace(File.separatorChar, '/');
return normalizedFileName;
StringgetPackageURI(String pkgName, String pkgPath, String currentPkgName)
Get the package URI to the given package name.
String newPackagePath;
if (pkgPath != null && !currentPkgName.equals(".")) {
    int indexOfCurrentPkgName = pkgPath.lastIndexOf(currentPkgName);
    if (indexOfCurrentPkgName >= 0) {
        newPackagePath = pkgPath.substring(0, indexOfCurrentPkgName);
    } else {
        newPackagePath = pkgPath;
    if (pkgName.equals(".")) {
        newPackagePath = Paths.get(newPackagePath).toString();
    } else {
        newPackagePath = Paths.get(newPackagePath, pkgName).toString();
} else {
    newPackagePath = pkgPath;
return newPackagePath;
PathgetPath(Class example, String filename)
get Path
final File f = new File(example.getProtectionDomain().getCodeSource().getLocation().getPath());
String path = f.getAbsolutePath();
int subPathIndex = path.indexOf("Examples");
String subPath = path.substring(0, subPathIndex) + "Data/" + filename;
Path p = Paths.get(subPath);
return p;
PathgetPath(Class baseClass, String resourceName)
get Path
URL url = baseClass.getResource(resourceName);
if (url == null) {
    throw new FileNotFoundException(String.format("Resource file is not found. %s", resourceName));
return Paths.get(url.toURI());
PathgetPath(File dir, String filename)
get Path
Path path = FileSystems.getDefault().getPath(dir.getAbsolutePath(), filename);
return path;
StringgetPath(final String fullFileName)
Returns the path (without the file-name) for a filename with path.
return Paths.get(fullFileName).getParent().toString();
PathgetPath(Object name)
get Path
Objects.requireNonNull(name);
if (name instanceof Path) {
    return (Path) name;
if (name instanceof File) {
    return ((File) name).toPath();
String p = name.toString();
...
StringgetPath(String dirName, String fileName)
get Path
return Paths.get(dirName, fileName).toString();
StringgetPathByClassName(Class clazz, String relativeDir)
Acquire a full path string by given class name and relative path string.
String javaSourcePath = System.getProperty("test.src").replaceAll("\\" + File.separator, FILE_SEP);
String normalizedPath = Paths.get(javaSourcePath, relativeDir).normalize().toAbsolutePath().toString();
return normalizedPath.replace("\\", FILE_SEP) + FILE_SEP;
PathgetPathByFilename(String filename, List files)
Find a file by it's filename.
for (Path file : files) {
    Path fname = file.getFileName();
    if (fname != null && fname.toString().equals(filename)) {
        return file;
return null;