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

ListsubDirectoryNames(final Path dir)
Gets the list of subdirectory names in the given directory path.
List<String> subdirs = new ArrayList<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir, Files::isDirectory)) {
    for (Path subDir : stream) {
        String conf = subDir.getFileName().toString();
        subdirs.add(conf);
    return subdirs;
StringtoClassName(String path)
to Class Name
StringBuilder sb = new StringBuilder();
sb.append(normalize(path).replace('-', '_').replace('.', '_').replace('/', '.'));
return sb.toString();
StringtoResourcePath(String fileName)
to Resource Path
return Paths.get("src", "main", "resources", fileName).toString();
voidunzipFile(String fileName, String targetPath)
Unzips a zip.
final File targetDir = new File(targetPath);
final ZipFile sourceZip = new ZipFile(fileName);
@SuppressWarnings("unchecked")
final Enumeration<ZipEntry> entries = (Enumeration<ZipEntry>) sourceZip.entries();
while (entries.hasMoreElements()) {
    ZipEntry currentEntry = entries.nextElement();
    File targetFile = new File(targetDir, currentEntry.getName());
    targetFile.getParentFile().mkdirs();
...
StringuploadFileToServer(InputStream input, String filename, String usercode, String basePath)
upload File To Server
Path cloudPath = Paths.get(URI.create(basePath + usercode + "/" + filename));
Files.copy(input, cloudPath, StandardCopyOption.REPLACE_EXISTING);
return ALIYUN_URL + cloudPath.toUri().toString();
booleanvalidateDockerfilePath(String name)
validate Dockerfile Path
return Files.exists(Paths.get(name)) && Files.isRegularFile(Paths.get(name))
        && Files.isReadable(Paths.get(name));
Pathwhich(Path path, String name)
which
if (isWindows()) {
    return path.resolve(name + ".bat");
} else {
    return path.resolve(name);
voidwriteDataFile(final String resourceName, final Path sourcePath)
write Data File
copy(Paths.get(currentThread().getContextClassLoader().getResource(resourceName).toURI()),
        new FileOutputStream(sourcePath.toFile(), false));
voidwriteFile(String pathname, String data)
Writes a string to the specified file using the default encoding.
write(new File(pathname), data, false);
intwriteFileFromInputStream(String the_path, String the_filename, InputStream the_sis)
Read raw bytes of a text stream and write the data to file specified by the filename.
int byteswritten = 0;
byte data[] = new byte[BUFFER];
int count = 0;
Path destpath = Paths.get(the_path + the_filename);
try (OutputStream fos = Files.newOutputStream(destpath, StandardOpenOption.CREATE);
        BufferedInputStream bis = new BufferedInputStream(the_sis, BUFFER)) {
    data = new byte[BUFFER];
    count = 0;
...