List of utility methods to do Path File Name nio
String | getNormalizedFileName(Path file) get Normalized File Name String normalizedFileName = file.toAbsolutePath().normalize().toString(); if (File.separatorChar != '/') { normalizedFileName = normalizedFileName.replace(File.separatorChar, '/'); return normalizedFileName; |
String | getPackageURI(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; |
Path | getPath(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; |
Path | getPath(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()); |
Path | getPath(File dir, String filename) get Path Path path = FileSystems.getDefault().getPath(dir.getAbsolutePath(), filename);
return path;
|
String | getPath(final String fullFileName) Returns the path (without the file-name) for a filename with path. return Paths.get(fullFileName).getParent().toString();
|
Path | getPath(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(); ... |
String | getPath(String dirName, String fileName) get Path return Paths.get(dirName, fileName).toString();
|
String | getPathByClassName(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; |
Path | getPathByFilename(String filename, List 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; |