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:Test.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:/home/docs/users.txt");
    BasicFileAttributeView view = Files.getFileAttributeView(path, BasicFileAttributeView.class);

    BasicFileAttributes attributes = view.readAttributes();
    FileTime lastModifedTime = attributes.lastModifiedTime();
    FileTime createTime = attributes.creationTime();

    long currentTime = Calendar.getInstance().getTimeInMillis();
    FileTime lastAccessTime = FileTime.fromMillis(currentTime);

    view.setTimes(lastModifedTime, lastAccessTime, createTime);
    System.out.println(attributes.lastAccessTime());
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("home/docs/users.txt");
    removePermission(path, PosixFilePermission.OWNER_EXECUTE);
}

From source file:Test.java

public static void main(String[] args) throws IOException {
    Path path = Paths.get("/users.txt");

    try (SeekableByteChannel sbc = Files.newByteChannel(path)) {
        ByteBuffer buffer = ByteBuffer.allocate(1024);

        sbc.position(4);//from w  w w .  j  av a  2  s .  c  om
        sbc.read(buffer);
        for (int i = 0; i < 5; i++) {
            System.out.print((char) buffer.get(i));
        }

        buffer.clear();
        sbc.position(0);
        sbc.read(buffer);
        for (int i = 0; i < 4; i++) {
            System.out.print((char) buffer.get(i));
        }
        sbc.position(0);
        buffer = ByteBuffer.allocate(1024);
        String encoding = System.getProperty("file.encoding");
        int numberOfBytesRead = sbc.read(buffer);
        System.out.println("Number of bytes read: " + numberOfBytesRead);
        while (numberOfBytesRead > 0) {
            buffer.rewind();
            System.out.print("[" + Charset.forName(encoding).decode(buffer) + "]");
            buffer.flip();
            numberOfBytesRead = sbc.read(buffer);
            System.out.println("\nNumber of bytes read: " + numberOfBytesRead);
        }

    }

}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("home/docs/users.txt");
    PosixFileAttributeView view = Files.getFileAttributeView(path, PosixFileAttributeView.class);

    PosixFileAttributes attributes = view.readAttributes();
    System.out.println("Group: " + attributes.group());
    System.out.println("Owner: " + attributes.owner().getName());

    Set<PosixFilePermission> permissions = attributes.permissions();
    for (PosixFilePermission permission : permissions) {
        System.out.print(permission.name() + " ");
    }//from   w w w.  j  a  v  a2s  .c  o m
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("home/docs/users.txt");
    listPermissions(path);/* w w w  .j ava  2 s.  co m*/
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(Paths.get("/asynchronous.txt"),
            StandardOpenOption.READ, StandardOpenOption.WRITE, StandardOpenOption.CREATE);
    CompletionHandler<Integer, Object> handler = new CompletionHandler<Integer, Object>() {

        @Override/*www . ja  v a2 s .  com*/
        public void completed(Integer result, Object attachment) {
            System.out.println("Attachment: " + attachment + " " + result + " bytes written");
            System.out.println("CompletionHandler Thread ID: " + Thread.currentThread().getId());
        }

        @Override
        public void failed(Throwable e, Object attachment) {
            System.err.println("Attachment: " + attachment + " failed with:");
            e.printStackTrace();
        }
    };

    System.out.println("Main Thread ID: " + Thread.currentThread().getId());
    fileChannel.write(ByteBuffer.wrap("Sample".getBytes()), 0, "First Write", handler);
    fileChannel.write(ByteBuffer.wrap("Box".getBytes()), 0, "Second Write", handler);

}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:/home/docs/users.txt");
    FileOwnerAttributeView view = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
    UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
    UserPrincipal userPrincipal = lookupService.lookupPrincipalByName("mary");

    view.setOwner(userPrincipal);/*  ww w .  j  a  v  a  2  s  . c o  m*/
    System.out.println("Owner: " + view.getOwner().getName());
}

From source file:Main.java

public static void main(String[] args) {

    Path path = Paths.get("C:/tutorial/Java/JavaFX");

    //no filter applyied
    try (DirectoryStream<Path> ds = Files.newDirectoryStream(path)) {
        for (Path file : ds) {
            System.out.println(file.getFileName());
        }/*from   w w  w . j  a  va 2s  . co  m*/
    } catch (IOException e) {
        System.err.println(e);
    }

}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:/home/docs/users.txt");
    FileOwnerAttributeView view = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
    UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
    UserPrincipal userPrincipal = lookupService.lookupPrincipalByName("mary");

    Files.setOwner(path, userPrincipal);
    System.out.println("Owner: " + view.getOwner().getName());

}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:/home/docs/users.txt");
    FileOwnerAttributeView view = Files.getFileAttributeView(path, FileOwnerAttributeView.class);
    UserPrincipal userPrincipal = view.getOwner();

    UserPrincipalLookupService lookupService = FileSystems.getDefault().getUserPrincipalLookupService();
    userPrincipal = lookupService.lookupPrincipalByName("users");
    view.setOwner(userPrincipal);//from  www.  java  2 s .  c  o m
    System.out.println("UserPrincipal set: " + userPrincipal.getName());

}