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) {
    Path dirToDelete = Paths.get("DIR");
    FileVisitor<Path> visitor = getFileVisitor();

    try {/*from  w ww.ja  v  a 2  s  . c om*/
        Files.walkFileTree(dirToDelete, visitor);
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

From source file:Test.java

public static void main(String args[]) throws IOException {
    Path path = Paths.get("home/docs");
    SecureDirectoryStream<Path> sds = (SecureDirectoryStream) Files.newDirectoryStream(path);
    PosixFileAttributeView view = sds.getFileAttributeView(PosixFileAttributeView.class);
    PosixFileAttributes attributes = view.readAttributes();
    Set<PosixFilePermission> permissions = attributes.permissions();

    for (PosixFilePermission permission : permissions) {
        System.out.print(permission.toString() + ' ');
    }//ww  w . j  a  v a  2s.co m
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("C:/home/docs/users.txt");
    AclFileAttributeView view = Files.getFileAttributeView(path, AclFileAttributeView.class);
    List<AclEntry> aclEntryList = view.getAcl();
    displayAclEntries(aclEntryList);/*from w w w  .j ava2  s  . c  om*/
}

From source file:Main.java

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

    Path path = Paths.get("C:\\Java_Dev\\test1.txt");

    FileOwnerAttributeView foav = Files.getFileAttributeView(path, FileOwnerAttributeView.class);

    UserPrincipal owner = foav.getOwner();
    System.out.format("Original owner  of  %s  is %s%n", path, owner.getName());

    FileSystem fs = FileSystems.getDefault();
    UserPrincipalLookupService upls = fs.getUserPrincipalLookupService();

    UserPrincipal newOwner = upls.lookupPrincipalByName("brice");
    foav.setOwner(newOwner);//from  w  w  w .  jav a  2 s .co  m

    UserPrincipal changedOwner = foav.getOwner();
    System.out.format("New owner  of  %s  is %s%n", path, changedOwner.getName());

}

From source file:Main.java

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

    try {/*from  www  .  j  a v  a  2  s.  c o m*/
        Path p = Files.move(source, target, StandardCopyOption.ATOMIC_MOVE);
        System.out.println(source + "  has  been  moved to " + p);
    } catch (NoSuchFileException e) {
        System.out.println("Source/target does  not  exist.");
    } catch (FileAlreadyExistsException e) {
        System.out.println(target + "  already exists.  Move failed.");
    } catch (DirectoryNotEmptyException e) {
        System.out.println(target + "  is not  empty.  Move failed.");
    } catch (AtomicMoveNotSupportedException e) {
        System.out.println("Atomic move is not  supported. MOve  failed.");
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Test.java

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

From source file:Test.java

public static void main(String[] args) {
    try {/*  ww  w  . ja  va 2 s  .c om*/
        Path source = Paths.get("/home");
        Path target = Paths.get("/backup");
        Files.walkFileTree(source, EnumSet.of(FileVisitOption.FOLLOW_LINKS), Integer.MAX_VALUE,
                new CopyDirectory(source, target));
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

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

    Path path = Paths.get("c:/home/tutorial/Java/JavaFX/Topic.txt");
    Path new_path = Paths.get("/home/tutorial/Java/JavaFX/new_Topic.txt");

    //use of fromString
    Set<PosixFilePermission> permissions = PosixFilePermissions.fromString("rw-r--r--");
    try {// w  w  w .  j  ava 2s.c  o  m
        Files.setPosixFilePermissions(new_path, permissions);
    } catch (IOException e) {
        System.err.println(e);
    }
}

From source file:Main.java

public static void main(String[] args) {
    try (WatchService ws = FileSystems.getDefault().newWatchService()) {
        Path dirToWatch = Paths.get("C:\\myName");
        dirToWatch.register(ws, StandardWatchEventKinds.ENTRY_CREATE, StandardWatchEventKinds.ENTRY_MODIFY,
                StandardWatchEventKinds.ENTRY_DELETE);
        while (true) {
            WatchKey key = ws.take();
            for (WatchEvent<?> event : key.pollEvents()) {
                Kind<?> eventKind = event.kind();
                if (eventKind == StandardWatchEventKinds.OVERFLOW) {
                    System.out.println("Event  overflow occurred");
                    continue;
                }//www . ja  va 2s  .co m
                WatchEvent<Path> currEvent = (WatchEvent<Path>) event;
                Path dirEntry = currEvent.context();
                System.out.println(eventKind + "  occurred on  " + dirEntry);
            }
            boolean isKeyValid = key.reset();
            if (!isKeyValid) {
                System.out.println("No  longer  watching " + dirToWatch);
                break;
            }
        }
    } catch (IOException | InterruptedException e) {
        e.printStackTrace();
    }
}

From source file:Main.java

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

    Path path = Paths.get("c:/home/tutorial/Java/JavaFX/Topic.txt");
    Path new_path = Paths.get("/home/tutorial/Java/JavaFX/new_Topic.txt");
    PosixFileAttributes attr = Files.readAttributes(path, PosixFileAttributes.class);
    attr = Files.getFileAttributeView(path, PosixFileAttributeView.class).readAttributes();

    //use of asFileAttribute
    FileAttribute<Set<PosixFilePermission>> posixattrs = PosixFilePermissions
            .asFileAttribute(attr.permissions());
    try {//from w w w.  j av  a 2 s  . co m
        Files.createFile(new_path, posixattrs);
    } catch (IOException e) {
        System.err.println(e);
    }
}