Java Utililty Methods Path Create nio

List of utility methods to do Path Create nio

Description

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

Method

ListcreateRandomFolders(Path basePath, int numberOfFolders)
create Random Folders
List<Path> folders = new ArrayList<>();
for (int i = 0; i < numberOfFolders; ++i) {
    Path folder = createRandomFolder(basePath);
    folders.add(folder);
return folders;
StringcreateRelativePath(File baseDirFile, File file)
create Relative Path
if (baseDirFile == null)
    throw new IllegalArgumentException("baseDirFile must not be null");
if (file == null)
    throw new IllegalArgumentException("file must not be null");
Path basePath = baseDirFile.toPath().normalize();
Path filePath = file.toPath().normalize();
return basePath.relativize(filePath).toString();
SourceFilecreateSourceFile(Path path, String... lines)
create Source File
return SourceFile.fromCode(path.toString(), Joiner.on("\n").join(lines));
voidcreateSymbolicLink(String linkPathString, String targetPathString)
create Symbolic Link
Class<?> fileSystemsClass = Class.forName("java.nio.file.FileSystems");
Class<?> fileSystemClass = Class.forName("java.nio.file.FileSystem");
Class<?> pathClass = Class.forName("java.nio.file.Path");
Class<?> filesClass = Class.forName("java.nio.file.Files");
Class<?> fileAttributeClass = Class.forName("java.nio.file.attribute.FileAttribute");
Object emptyFileAttributeArray = Array.newInstance(fileAttributeClass, 0);
Method fileSystemsGetDefaultMethod = fileSystemsClass.getMethod("getDefault");
Method fileSystemGetPathMethod = fileSystemClass.getMethod("getPath", String.class, String[].class);
...
voidcreateSymlink(Path realFileLocation, Path softLinkLocation)
create Symlink
try {
    Files.createSymbolicLink(softLinkLocation, realFileLocation);
} catch (IOException e) {
    System.err.println(e);
} catch (UnsupportedOperationException e) {
    System.err.println(e);
voidcreateSymlink(String targetPathStr, File symlinkFile)
create Symlink
Path targetPath = Paths.get(targetPathStr);
Path symlinkPath = Paths.get(symlinkFile.getPath());
Files.createSymbolicLink(symlinkPath, targetPath);
PathcreateTempDirectory(final String path, final String prefix)
create Temp Directory
FileSystem fs = FileSystems.getDefault();
Path temp = Files.createTempDirectory(fs.getPath(path), prefix);
return temp;
PathcreateTempDirectory(Path dir, String prefix)
Create a temporary directory with the same attributes as its parent directory.
try {
    return Files.createTempDirectory(dir, prefix);
} catch (UnsupportedOperationException ex) {
return Files.createTempDirectory(dir, prefix);
PathcreateTempFile(Path baseDir, String prefix, String suffix)
create Temp File
Path tmp = Files.createTempFile(baseDir, prefix, suffix);
tmp.toFile().deleteOnExit();
return tmp;
PathcreateTempFilePath(final String stem, final String extension)
create Temp File Path
final Path tempFilePath = createTempFile("schemacrawler." + stem + ".", "." + extension).normalize()
        .toAbsolutePath();
delete(tempFilePath);
tempFilePath.toFile().deleteOnExit();
return tempFilePath;