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

voidcreateMainClassAndBuildFileWithDeps(String targetName, String deps, Path dir)
create Main Class And Build File With Deps
writeLinesToFile(dir.resolve("Main" + CPP_FILE_SUFFIX), "int main() {", "  return 0;", "}");
appendLinesToFile(dir.resolve(BUILD_FILE_NAME), "cc_binary(", "    name = '" + targetName + "',",
        "    srcs = [ 'Main.cc' ],", deps, ")");
voidcreateOverwriteDirectory(Path path)
Creates new directory or overwrites existing one.
if (Files.exists(path)) {
    delete(path);
Files.createDirectory(path);
PathcreatePath(String fileName, String subfolder)
create Path
return Paths.get(subfolder, UUID.randomUUID() + "-" + fileName.replace("/", "-").replace("\\", "-"));
ComparatorcreatePathComparator()
create Path Comparator
return new Comparator<Path>() {
    @Override
    public int compare(Path p1, Path p2) {
        String s1 = (p1 != null ? p1.toString() : null);
        String s2 = (p2 != null ? p2.toString() : null);
        if (s1 == null) {
            s1 = "";
        return s1.compareTo(s2 != null ? s2 : "");
};
PathMatcher[]createPathMatcher(String[] patterns)
create Path Matcher
if (patterns == null) {
    return new PathMatcher[0];
int matcherSize = patterns.length;
PathMatcher[] matchers = new PathMatcher[matcherSize];
for (int i = 0; i < matcherSize; i++) {
    matchers[i] = FileSystems.getDefault().getPathMatcher("glob:" + patterns[i]);
return matchers;
PathcreatePathOrNull(String pathString)
create Path Or Null
try {
    return Paths.get(pathString);
} catch (InvalidPathException ipe) {
    return null;
FunctioncreatePathRelativizer(Path path, boolean doRelativize)
create Path Relativizer
return (p) -> (doRelativize ? path.relativize(p) : p);
PathcreateProperties(final Path directory, final Properties properties)
create Properties
Objects.requireNonNull(directory, "directory");
Objects.requireNonNull(properties, "properties");
if (!Files.isDirectory(directory)) {
    throw new IllegalArgumentException("directory " + directory + " must be a directory");
Path configurationFile = directory.resolve(getConfigurationFileName());
properties.store(Files.newOutputStream(configurationFile), "configuration");
return configurationFile;
...
voidcreateRandomClass(String className, Path dir)
create Random Class
writeLinesToFile(dir.resolve(className + CPP_FILE_SUFFIX), "#include <random>", "#include <iostream>",
        "#include <ctime>", "using namespace std;", "", "class " + className + " {", "public:",
        "  static void printSth() {", "    srand(time(NULL));", "    int n = rand();",
        "    cout << \"This is method(printSth) with random number(\" << n << \")\\n\";", "  }", "};");
writeLinesToFile(dir.resolve(className + CPP_HEADER_FILE_SUFFIX), "class " + className + " {", "public:",
        "  static void printSth();", "};");
PathcreateRandomFolder(Path basePath)
create Random Folder
String folderName = "f" + FOLDER_NUMBER++;
Path p = basePath.resolve(folderName);
Files.createDirectory(p);
return p;