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

voidcreateDirectory(Path path)
create Directory
if (Files.notExists(path)) {
    Files.createDirectory(path);
voidcreateDirIfDoesNotExist(String websitesDirPath)
create Dir If Does Not Exist
Path directoryPath = Paths.get(websitesDirPath);
if (!Files.exists(directoryPath)) {
    try {
        Files.createDirectory(directoryPath);
    } catch (IOException e) {
        e.printStackTrace();
voidcreateEmptyResourceFile(Path path)
Creates an empty resource file at the given path, assuming the file does not exist already
if (path.toFile().exists()) {
    return;
try {
    Files.createFile(path);
    Files.write(path, EMPTY_RESOURCE_FILE.getBytes());
} catch (IOException e) {
    e.printStackTrace();
...
voidcreateFile(File root, String path, String filename, String value)
create File
File dir = new File(root, path);
if (!dir.exists()) {
    if (!dir.mkdirs()) {
        throw new IOException("Failed to create directories " + dir.toString());
File file = new File(dir, filename);
if (!file.createNewFile()) {
...
voidcreateFile(final Path file, final String line)
create File
try (final PrintWriter pw = new PrintWriter(Files.newBufferedWriter(file, StandardCharsets.UTF_8), true)) {
    pw.println(line);
voidcreateFile(Path filePath, byte[] content)
Creates a file with the specified content.
try (OutputStream output = Files.newOutputStream(filePath)) {
    output.write(content);
    output.flush();
voidcreateFile(Path p)
create File
try {
    PrintWriter writer = new PrintWriter(p.toString(), "UTF-8");
    writer.println(p.toString());
    writer.close();
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (UnsupportedEncodingException e) {
    e.printStackTrace();
...
FilecreateFile(Path path)
Create a file at the given absolute or relative path.
File file = path.toAbsolutePath().toFile();
if (file.exists() && file.canRead() && file.canWrite()) {
    if (file.isFile())
        return file;
    throw new IllegalStateException("Expecting '" + path + "' to be a file but found a directory");
file.getParentFile().mkdirs();
return file;
...
voidcreateFile(Path path)
create File
try {
    Files.createFile(path);
} catch (FileAlreadyExistsException e) {
} catch (IOException e) {
    throw new RuntimeException(e);
voidcreateFile(Path path)
Creates a new and empty file.
Files.createFile(path);