Java Path Create nio createFile(Path path)

Here you can find the source of createFile(Path path)

Description

Create a file at the given absolute or relative path.

License

Apache License

Parameter

Parameter Description
path the relative or absolute path of the file to create; may not be null

Return

the reference to the existing readable and writable file

Declaration

public static File createFile(Path path) 

Method Source Code

//package com.java2s;
/*//from   w  ww  . j  a  v  a  2 s.  com
 * Copyright 2015 Red Hat, Inc. and/or its affiliates.
 * 
 * Licensed under the Apache Software License version 2.0, available at http://www.apache.org/licenses/LICENSE-2.0
 */

import java.io.File;

import java.nio.file.Path;

public class Main {
    /**
     * Create a file at the given absolute or relative path.
     * 
     * @param path the relative or absolute path of the file to create; may not be null
     * @return the reference to the existing readable and writable file
     */
    public static File createFile(Path 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;
    }
}

Related

  1. createEmptyResourceFile(Path path)
  2. createFile(File root, String path, String filename, String value)
  3. createFile(final Path file, final String line)
  4. createFile(Path filePath, byte[] content)
  5. createFile(Path p)
  6. createFile(Path path)
  7. createFile(Path path)
  8. createFile(Path path)
  9. createFile(String filePath, String content)