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("/users.txt");
    byte[] contents = Files.readAllBytes(path);

    Path newPath = Paths.get("/newUsers.txt");
    byte[] newContents = "aaa".getBytes();
    Files.write(newPath, contents, StandardOpenOption.CREATE);
    Files.write(newPath, newContents, StandardOpenOption.APPEND);
}

From source file:Main.java

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

    Path newPath = Paths.get("/newUsers.txt");

    Files.write(newPath, contents, StandardOpenOption.CREATE);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    BufferedReader reader = Files.newBufferedReader(Paths.get("a.bat"), StandardCharsets.UTF_8);
    //BufferedReader's lines methods returns a stream of all lines
    reader.lines().forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {
    final String dir = "E:\\Java_Dev\\";
    final File file = Paths.get(dir).toFile();
    File[] files = file.listFiles();
    Arrays.sort(files, (f1, f2) -> {
        if (f1.isDirectory() != f2.isDirectory()) {
            if (f1.isDirectory()) {
                return -1;
            } else {
                return 1;
            }//from  ww w .  j  a  va 2  s . co  m
        } else {
            return f1.getName().toLowerCase().compareTo(f2.getName().toLowerCase());
        }
    });
    Stream.of(files).forEach(System.out::println);
}

From source file:Main.java

public static void main(String[] args) {
    final String dir = "C:\\data\\projects\\";
    final String pattern = ".pdf";
    final File file = Paths.get(dir).toFile();

    Stream.of(file.list((pFile, pString) -> pString.endsWith(pattern))).forEach(System.out::println);
    ;/*w  w  w .j a  v a  2s . c o  m*/
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path src = Paths.get("test2.txt");
    SeekableByteChannel sbc = Files.newByteChannel(src, StandardOpenOption.READ, StandardOpenOption.WRITE,
            StandardOpenOption.CREATE);
}

From source file:Main.java

public static void main(String[] args) {
    // Assume that C:\Java_Dev\test1.txt file exists
    Path p1 = Paths.get("C:\\Java_Dev\\test1.txt");
    Path p2 = Paths.get("C:\\Java_Dev\\..\\Java_Dev\\test1.txt");

    // Assume that C:\abc.txt file does not exist
    Path p3 = Paths.get("C:\\abc.txt");
    Path p4 = Paths.get("C:\\abc.txt");

    try {//from w  w w . j  av a  2  s  .  co  m
        boolean isSame = Files.isSameFile(p1, p2);
        System.out.println("p1 and  p2  are   the   same:  " + isSame);

        isSame = Files.isSameFile(p3, p4);
        System.out.println("p3 and  p4  are   the   same:  " + isSame);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:Test.java

public static void main(String[] args) throws IOException {
    Path path = Paths.get("/home/docs/users.txt");
    List<String> contents = Files.readAllLines(path, Charset.defaultCharset());
    for (String b : contents) {
        System.out.println(b);//from w  w w  .jav  a 2s.  c  om
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    Path path = Paths.get("");
    FileStore fs = Files.getFileStore(path);

    // Check if POSIX file attribute is supported by the file store
    boolean supported = fs.supportsFileAttributeView(PosixFileAttributeView.class);
    if (supported) {
        System.out.println("POSIX file attribute view  is supported.");
    } else {/*from w w  w . jav a2 s  . c  om*/
        System.out.println("POSIX file attribute view  is not  supported.");
    }

}

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();
    System.out.println(userPrincipal.getName());
}