Java Path Create nio createClassWithDepsNextExtra(int index, Path dir)

Here you can find the source of createClassWithDepsNextExtra(int index, Path dir)

Description

create Class With Deps Next Extra

License

Open Source License

Declaration

static void createClassWithDepsNextExtra(int index, Path dir) throws IOException 

Method Source Code


//package com.java2s;
// Licensed under the Apache License, Version 2.0 (the "License");

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.CREATE;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class Main {
    private static final String CPP_FILE_SUFFIX = ".cc";
    private static final String CPP_HEADER_FILE_SUFFIX = ".h";

    static void createClassWithDepsNextExtra(int index, Path dir) throws IOException {
        writeLinesToFile(dir.resolve("Deps" + index + CPP_HEADER_FILE_SUFFIX), "class Deps" + index + " {",
                "public:", "  static void printSth();", "  static void printSthElse();",
                "  static void callNext();", "};");
        writeLinesToFile(dir.resolve("Deps" + index + CPP_FILE_SUFFIX), "#include <random>", "#include <iostream>",
                "#include <ctime>", "#include \"Deps" + (index + 1) + ".h\"", "using namespace std;", "",
                "class Deps" + index + " {", "public:", "  static void printSth() {", "    srand(time(NULL));",
                "    int n = rand();",
                "    cout << \"This is method(printSth) with random number(\" << n << \")\\n\";", "  }",
                "  static void printSthElse() {", "    srand(time(NULL));", "    int n = rand();",
                "    cout << \"This is method(printSthElse) with random number(\" << n << \")\\n\";", "  }",
                "  static void callNext() {", "    Deps" + (index + 1) + "::printSth();", "  }", "};");
    }/* w  w  w.  j  a  v a  2s.co m*/

    private static void writeLinesToFile(Path filePath, String... lines) throws IOException {
        writeOrAppendLinesToFile(false, filePath, lines);
    }

    private static void writeOrAppendLinesToFile(boolean append, Path filePath, String... lines)
            throws IOException {
        File file = filePath.toFile();
        if (!file.exists() && !file.createNewFile()) {
            return;
        }

        PrintWriter printWriter = new PrintWriter(Files.newBufferedWriter(file.toPath(), UTF_8,
                append ? new StandardOpenOption[] { CREATE, APPEND } : new StandardOpenOption[] { CREATE }));
        for (String line : lines) {
            printWriter.println(line);
        }
        printWriter.close();
    }
}

Related

  1. clearAndCreate(Path dir)
  2. createACL(Path root, Path p, GroupPrincipal readOnly, GroupPrincipal readWrite)
  3. createAndGetOutputStream(String typename, String outputPath)
  4. createBufferedReader(Path path)
  5. createChecksums(final Path file, final Map functions)
  6. createDir(Path p)
  7. createDirectoriesIfRequired(@Nonnull Path path)
  8. createDirectory(final Path parent, final String folderName)
  9. createDirectory(Path parent, String name)