Java IO Tutorial - Java Files.createFile(Path path, FileAttribute <?>... attrs)








Syntax

Files.createFile(Path path, FileAttribute <?>... attrs) has the following syntax.

public static Path createFile(Path path,  FileAttribute <?>... attrs)   throws IOException

Example

In the following code shows how to use Files.createFile(Path path, FileAttribute <?>... attrs) method.

import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.FileAttribute;
import java.nio.file.attribute.PosixFilePermission;
import java.nio.file.attribute.PosixFilePermissions;
import java.util.Set;
/*w  ww  .  j  ava 2  s .  co  m*/

public class Main {

    public static void main(String[] args) {

        Path newfile_2 = FileSystems.getDefault().getPath("/home/tutorial/Java/2010/demo.txt");

        //create a file with a set of specified attributes
        Set<PosixFilePermission> perms = PosixFilePermissions.fromString("rw-------");
        FileAttribute<Set<PosixFilePermission>> attr = PosixFilePermissions.asFileAttribute(perms);
        try {
            Files.createFile(newfile_2, attr);
        } catch (IOException e) {
            System.err.println(e);
        }

    }
}

The code above generates the following result.