Example usage for java.nio.file Paths get

List of usage examples for java.nio.file Paths get

Introduction

In this page you can find the example usage for java.nio.file Paths get.

Prototype

public static Path get(URI uri) 

Source Link

Document

Converts the given URI to a Path object.

Usage

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("test");
    PosixFileAttributeView posixView = Files.getFileAttributeView(path, PosixFileAttributeView.class);
    if (posixView == null) {
        System.out.format("POSIX attribute view  is not  supported%n.");
        return;//from  w w  w .j  av  a  2  s.  c o m
    }
    readPermissions(posixView);
    updatePermissions(posixView);
}

From source file:Main.java

public static void main(String[] args) {
    DirectoryStream.Filter<Path> filter = new DirectoryStream.Filter<Path>() {
        public boolean accept(Path file) throws IOException {
            return (Files.isHidden(file));
        }//from www  .java  2 s  . com
    };
    Path directory = Paths.get("C:/Windows");
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory, filter)) {
        for (Path file : directoryStream) {
            System.out.println(file.getFileName());
        }
    } catch (IOException | DirectoryIteratorException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    FileSystem fileSystem = FileSystems.getDefault();
    WatchService watchService = fileSystem.newWatchService();
    Path directory = Paths.get("c:/");
    WatchEvent.Kind<?>[] events = { StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE,
            StandardWatchEventKinds.ENTRY_MODIFY };
    directory.register(watchService, events);
    while (true) {
        System.out.println("Waiting for a watch event");
        WatchKey watchKey = watchService.take();

        System.out.println("Path being watched: " + watchKey.watchable());
        System.out.println();//w w w.ja v  a  2  s .c om

        if (watchKey.isValid()) {
            for (WatchEvent<?> event : watchKey.pollEvents()) {
                System.out.println("Kind: " + event.kind());
                System.out.println("Context: " + event.context());
                System.out.println("Count: " + event.count());
                System.out.println();
            }

            boolean valid = watchKey.reset();
            if (!valid) {
                // The watchKey is not longer registered
            }
        }
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String dirPrefix = "KDir";
    Path tDir = Files.createTempDirectory(dirPrefix);
    System.out.println("Temp directory: " + tDir);
    String fPrefix = "Header_";
    String fSuffix = ".txt";
    Path tFile1 = Files.createTempFile(fPrefix, fSuffix);
    System.out.println("Temp file1: " + tFile1);

    Path p1 = Paths.get("C:\\temp");
    Path tFile2 = Files.createTempFile(p1, fPrefix, fSuffix);
    System.out.println("Temp file2: " + tFile2);
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    FileSystem fileSystem = FileSystems.getDefault();
    WatchService watchService = fileSystem.newWatchService();
    Path directory = Paths.get("/home/docs");
    WatchEvent.Kind<?>[] events = { StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE,
            StandardWatchEventKinds.ENTRY_MODIFY };
    directory.register(watchService, events);
    while (true) {
        System.out.println("Waiting for a watch event");
        WatchKey watchKey = watchService.take();
        System.out.println("Path being watched: " + watchKey.watchable());
        if (watchKey.isValid() == false) {
            return;
        }//from   www. j  a  v  a 2s . c om
        for (WatchEvent<?> event : watchKey.pollEvents()) {
            System.out.println("Kind: " + event.kind());
            System.out.println("Context: " + event.context());
            System.out.println("Count: " + event.count());
            System.out.println();
        }
        boolean valid = watchKey.reset();
        System.out.println(valid);

    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:\\Java_Dev\\test1.txt");

    AclFileAttributeView aclView = Files.getFileAttributeView(path, AclFileAttributeView.class);
    if (aclView == null) {
        System.out.format("ACL view  is not  supported.%n");
        return;/*ww  w  .ja va 2 s.  c  o  m*/
    }
    UserPrincipal bRiceUser = FileSystems.getDefault().getUserPrincipalLookupService()
            .lookupPrincipalByName("brice");

    Set<AclEntryPermission> permissions = EnumSet.of(AclEntryPermission.READ_DATA,
            AclEntryPermission.WRITE_DATA);

    AclEntry.Builder builder = AclEntry.newBuilder();
    builder.setPrincipal(bRiceUser);
    builder.setType(AclEntryType.ALLOW);
    builder.setPermissions(permissions);
    AclEntry newEntry = builder.build();

    List<AclEntry> aclEntries = aclView.getAcl();

    aclEntries.add(newEntry);

    aclView.setAcl(aclEntries);
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileSystem fileSystem = FileSystems.getDefault();
    WatchService watchService = fileSystem.newWatchService();
    Path directory = Paths.get("c:/");
    WatchEvent.Kind<?>[] events = { StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_DELETE,
            StandardWatchEventKinds.ENTRY_MODIFY };
    directory.register(watchService, events);
    while (true) {
        System.out.println("Waiting for a watch event");
        WatchKey watchKey = watchService.take();

        System.out.println("Path being watched: " + watchKey.watchable());
        System.out.println();//  ww w.j ava  2 s  . c  o m

        if (watchKey.isValid()) {
            for (WatchEvent<?> event : watchKey.pollEvents()) {
                System.out.println("Kind: " + event.kind());
                System.out.println("Context: " + event.context());
                System.out.println("Count: " + event.count());
                System.out.println();
            }

            boolean valid = watchKey.reset();
            if (!valid) {
                // The watchKey is not longer registered
            }
        }
    }

}

From source file:Test.java

public static void main(String[] args) throws Exception {

    Path profile = Paths.get("/user/Admin/.profile");

    PosixFileAttributes attrs = Files.readAttributes(profile, PosixFileAttributes.class);

    Set<PosixFilePermission> posixPermissions = attrs.permissions();
    posixPermissions.clear();/* ww w .  j ava2  s. com*/

    String owner = attrs.owner().getName();
    String perms = PosixFilePermissions.toString(posixPermissions);
    System.out.format("%s %s%n", owner, perms);

    posixPermissions.add(OWNER_READ);
    posixPermissions.add(GROUP_READ);
    posixPermissions.add(OWNER_READ);
    posixPermissions.add(OWNER_WRITE);
    Files.setPosixFilePermissions(profile, posixPermissions);

}

From source file:Main.java

public static void main(String[] args) {
    Path src = Paths.get("test.txt");
    String encoding = System.getProperty("file.encoding");
    Charset cs = Charset.forName(encoding);
    try (SeekableByteChannel seekableChannel = Files.newByteChannel(src, READ, WRITE, CREATE,
            TRUNCATE_EXISTING)) {//from   www. j  a  va  2s  .c  o m
        printDetails(seekableChannel, "Before writing data");
        writeData(seekableChannel, cs);
        printDetails(seekableChannel, "After writing data");

        seekableChannel.position(0);
        printDetails(seekableChannel, "After resetting position to 0");
        readData(seekableChannel, cs);
        printDetails(seekableChannel, "After reading data");
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Test.java

public static void main(String args[]) throws Exception {
    ExecutorService pool = new ScheduledThreadPoolExecutor(3);
    AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("data.txt"),
            EnumSet.of(StandardOpenOption.READ), pool);
    CompletionHandler<Integer, ByteBuffer> handler = new CompletionHandler<Integer, ByteBuffer>() {
        public synchronized void completed(Integer result, ByteBuffer attachment) {
            for (int i = 0; i < attachment.limit(); i++) {
                System.out.print((char) attachment.get(i));
            }/* www  .j  a v  a2s  .  c  om*/
        }

        public void failed(Throwable e, ByteBuffer attachment) {
        }
    };
    final int bufferCount = 5;
    ByteBuffer buffers[] = new ByteBuffer[bufferCount];
    for (int i = 0; i < bufferCount; i++) {
        buffers[i] = ByteBuffer.allocate(10);
        fileChannel.read(buffers[i], i * 10, buffers[i], handler);
    }
    pool.awaitTermination(1, TimeUnit.SECONDS);
    for (ByteBuffer byteBuffer : buffers) {
        for (int i = 0; i < byteBuffer.limit(); i++) {
            System.out.println((char) byteBuffer.get(i));
        }
    }
}