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");

    UserDefinedFileAttributeView view = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class);
    view.write("publishable", Charset.defaultCharset().encode("true"));
    System.out.println("Publishable set");

    String name = "publishable";
    ByteBuffer buffer = ByteBuffer.allocate(view.size(name));
    view.read(name, buffer);/* w  w  w  .  ja v  a2s .c om*/
    buffer.flip();
    String value = Charset.defaultCharset().decode(buffer).toString();
    System.out.println(value);

}

From source file:Test.java

public static void main(String[] args) throws IOException {
    Path file = Paths.get("/home/docs/users.txt");
    Path newFile = Paths.get("/home/docs/newUsers.txt");
    try (InputStream in = Files.newInputStream(file);
            OutputStream out = Files.newOutputStream(newFile, StandardOpenOption.CREATE,
                    StandardOpenOption.APPEND)) {
        int data = in.read();
        while (data != -1) {
            out.write(data);/*from   w ww .j  av a 2  s. c o  m*/
            data = in.read();
        }
    }

}

From source file:Main.java

public static void main(String[] args) {
    String globPattern = "glob:**txt";
    PathMatcher matcher = FileSystems.getDefault().getPathMatcher(globPattern);
    Path path = Paths.get("C:\\Java_Dev\\test1.txt");
    boolean matched = matcher.matches(path);
    System.out.format("%s matches  %s:  %b%n", globPattern, path, matched);
}

From source file:Test.java

License:asdf

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

    final String newLine = System.getProperty("line.separator");
    try (SeekableByteChannel sbc = Files.newByteChannel(path, StandardOpenOption.APPEND)) {
        String output = newLine + "asdf" + newLine;
        ByteBuffer buffer = ByteBuffer.wrap(output.getBytes());
        sbc.write(buffer);// ww w .ja  va 2s . com
    }

}

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);

    Future<Integer> writeFuture1 = fileChannel.write(ByteBuffer.wrap("Sample".getBytes()), 0);
    Future<Integer> writeFuture2 = fileChannel.write(ByteBuffer.wrap("Box".getBytes()), 0);

    int result;/*from w  w  w  .  j av  a2 s.  c om*/
    result = writeFuture1.get();
    System.out.println("Sample write completed with " + result + " bytes written");
    result = writeFuture2.get();
    System.out.println("Box write completed with " + result + " bytes written");

}

From source file:Test.java

License:asdf

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

    final String newLine = System.getProperty("line.separator");
    try (SeekableByteChannel sbc = Files.newByteChannel(path, StandardOpenOption.WRITE)) {
        ByteBuffer buffer;/*w  w  w  .  ja  v  a2s.  co m*/
        long position = sbc.size();
        sbc.position(position);
        System.out.println("Position: " + sbc.position());

        buffer = ByteBuffer.wrap((newLine + "asdf").getBytes());
        sbc.write(buffer);
        System.out.println("Position: " + sbc.position());
        buffer = ByteBuffer.wrap((newLine + "asdf").getBytes());
        sbc.write(buffer);
        System.out.println("Position: " + sbc.position());
    }
}

From source file:Test.java

public static void main(String[] args) throws IOException {
    Path startingDir = Paths.get("/Users/java");
    Files.walkFileTree(startingDir, new FindJavaVisitor());
}

From source file:Test.java

License:asdf

public static void main(String[] args) throws IOException {
    String newName = "asdf";
    Path file = Paths.get("/users.txt");
    try (BufferedWriter writer = Files.newBufferedWriter(file, Charset.defaultCharset(),
            StandardOpenOption.APPEND)) {
        writer.newLine();/*  w w  w. j a  v a 2 s  . co m*/
        writer.write(newName, 0, newName.length());
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Charset cs = Charset.forName("US-ASCII");
    Path source = Paths.get("test1.txt");

    List<String> lines = Files.readAllLines(source, cs);
    for (String line : lines) {
        System.out.println(line);
    }/*from  w  ww .  j  a  v a 2s.  c  o  m*/
}

From source file:Test.java

public static void main(String[] args) {
    Path directory = Paths.get("C:/Program Files/Java/jdk1.7.0/bin");
    PathMatcher pathMatcher = FileSystems.getDefault().getPathMatcher("glob:java?.exe");
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(directory, "java*.exe")) {
        for (Path file : directoryStream) {
            if (pathMatcher.matches(file.getFileName())) {
                System.out.println(file.getFileName());
            }/*from   www .  j ava  2s .  c  o m*/
        }
    } catch (IOException | DirectoryIteratorException ex) {
        ex.printStackTrace();
    }

}