Tutorial by Examples

The Path class is used to programmaticaly represent a path in the file system (and can therefore point to files as well as directories, even to non-existent ones) A path can be obtained using the helper class Paths: Path p1 = Paths.get("/var/www"); Path p2 = Paths.get(URI.create("f...
Information about a path can be get using the methods of a Path object: toString() returns the string representation of the path Path p1 = Paths.get("/var/www"); // p1.toString() returns "/var/www" getFileName() returns the file name (or, more specifically, the last ...
Joining Two Paths Paths can be joined using the resolve() method. The path passed has to be a partial path, which is a path that doesn't include the root element. Path p5 = Paths.get("/home/"); Path p6 = Paths.get("arthur/files"); Path joined = p5.resolve(p6); Path otherJoi...
To interact with the filesystem you use the methods of the class Files. Checking existence To check the existence of the file or directory a path points to, you use the following methods: Files.exists(Path path) and Files.notExists(Path path) !Files.exists(path) does not neccesarily have t...
Files can be read byte- and line-wise using the Files class. Path p2 = Paths.get(URI.create("file:///home/testuser/File.txt")); byte[] content = Files.readAllBytes(p2); List<String> linesOfContent = Files.readAllLines(p2); Files.readAllLines() optionally takes a charset as para...
Files can be written bite- and line-wise using the Files class Path p2 = Paths.get("/home/testuser/File.txt"); List<String> lines = Arrays.asList( new String[]{"First line", "Second line", "Third line"}); Files.write(p2, lines); Files.writ...

Page 1 of 1