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 IOException {
    Path path = Paths.get("/home/docs/users.txt");
    Charset charset = Charset.forName("ISO-8859-1");
    try (BufferedReader reader = Files.newBufferedReader(path, charset)) {
        String line = null;/* www .j a  v  a2s  . c  o m*/
        while ((line = reader.readLine()) != null) {
            System.out.println(line);
        }
    }
}

From source file:Test.java

public static void main(String[] args) throws Exception {
    Path file = Paths.get("/usr/a.txt");
    AsynchronousFileChannel channel = AsynchronousFileChannel.open(file);

    ByteBuffer buffer = ByteBuffer.allocate(100_000);
    Future<Integer> result = channel.read(buffer, 0);

    while (!result.isDone()) {
        ProfitCalculator.calculateTax();
    }//w ww .  j a va 2  s.  c  o  m
    Integer bytesRead = result.get();
    System.out.println("Bytes read [" + bytesRead + "]");
}

From source file:Main.java

public static void main(String[] args) {
    Path directory = Paths.get("c:/");
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) {
        for (Path file : directoryStream) {
            System.out.println(file.getFileName());
        }//from w  w w.  j ava  2  s  . com
    } catch (IOException | DirectoryIteratorException ex) {
        ex.printStackTrace();
    }
}

From source file:Test.java

public static void main(String[] args) {
    Path directory = Paths.get("/home");
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory)) {
        for (Path file : directoryStream) {
            System.out.println(file.getFileName());
        }//from   w  w  w  .ja  va2 s  .c o  m
    } catch (IOException | DirectoryIteratorException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    Path p1 = Paths.get("test.txt");
    try {//from   ww  w  . j av a2 s .  c  om
        Files.createFile(p1);
        System.out.format("File created:  %s%n", p1.toRealPath());
    } catch (FileAlreadyExistsException e) {
        System.out.format("File %s  already exists.%n", p1.normalize());
    } catch (NoSuchFileException e) {
        System.out.format("Directory %s  does  not  exists.%n", p1.normalize().getParent());
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Test.java

public static void main(String[] args) {
    Path directory = Paths.get("C:/Program Files/Java/jdk1.7.0/bin");
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory, "java*.exe")) {
        for (Path file : directoryStream) {
            System.out.println(file.getFileName());
        }//from w  w  w  . j a v a 2s  .  c  om
    } catch (IOException | DirectoryIteratorException 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");

    PosixFileAttributes attr = Files.readAttributes(path, PosixFileAttributes.class);
    attr = Files.getFileAttributeView(path, PosixFileAttributeView.class).readAttributes();

    System.out.println("File owner: " + attr.owner().getName());
    System.out.println("File group: " + attr.group().getName());
    System.out.println("File permissions: " + attr.permissions().toString());

}

From source file:Main.java

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

    try {/* ww  w  .  j  a  v  a  2  s.c  o m*/
        Files.delete(p);
        System.out.println(p + "  deleted successfully.");
    } catch (NoSuchFileException e) {
        System.out.println(p + "  does  not  exist.");
    } catch (DirectoryNotEmptyException e) {
        System.out.println("Directory " + p + "  is not  empty.");
    } catch (IOException e) {
        e.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {

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

    DirectoryStream.Filter<Path> dir_filter = new DirectoryStream.Filter<Path>() {
        public boolean accept(Path path) throws IOException {
            return (Files.isDirectory(path, NOFOLLOW_LINKS));
        }//  ww  w.j a  v  a2 s  . c o m
    };

    try (DirectoryStream<Path> ds = Files.newDirectoryStream(path, dir_filter)) {
        for (Path file : ds) {
            System.out.println(file.getFileName());
        }
    } catch (IOException e) {
        System.err.println(e);
    }

}

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);//  w ww. j ava 2 s  . c o  m
        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));
        }

    }
}