New File I/O
Manipulating paths
Section titled “Manipulating paths”Joining Two Paths
Section titled “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 otherJoined = p5.resolve("ford/files");joined.toString() == "/home/arthur/files"otherJoined.toString() == "/home/ford/files"Normalizing a path
Section titled “Normalizing a path”Paths may contain the elements . (which points to the directory you’re currently in) and ..(which points to the parent directory).
When used in a path, . can be removed at any time without changing the path’s destination, and .. can be removed together with the preceding element.
With the Paths API, this is done using the .normalize() method:
Path p7 = Paths.get("/home/./arthur/../ford/files");Path p8 = Paths.get("C:\\Users\\.\\..\\Program Files");p7.normalize().toString() == "/home/ford/files"p8.normalize().toString() == "C:\\Program Files"Creating paths
Section titled “Creating paths”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("file:///home/testuser/File.txt"));Path p3 = Paths.get("C:\\Users\\DentAr\\Documents\\HHGTDG.odt");Path p4 = Paths.get("/home", "arthur", "files", "diary.tex");Retrieving information about a path
Section titled “Retrieving information about a path”Information about a path can be get using the methods of a Path object:
Path p1 = Paths.get("/var/www"); // p1.toString() returns "/var/www"Path p1 = Paths.get("/var/www"); // p1.getFileName() returns "www"Path p3 = Paths.get("C:\\Users\\DentAr\\Documents\\HHGTDG.odt"); // p3.getFileName() returns "HHGTDG.odt"Path p1 = Paths.get("/var/www"); // p1.getNameCount() returns 2Path p1 = Paths.get("/var/www"); // p1.getName(0) returns "var", p1.getName(1) returns "www"Path p1 = Paths.get("/var/www"); // p1.getParent().toString() returns "/var"Path p1 = Paths.get("/var/www"); // p1.getRoot().toString() returns "/"Path p3 = Paths.get("C:\\Users\\DentAr\\Documents\\HHGTDG.odt"); // p3.getRoot().toString() returns "C:\\"Retrieving information using the filesystem
Section titled “Retrieving information using the filesystem”To interact with the filesystem you use the methods of the class Files.
Checking existence
Section titled “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 to be equal to Files.notExists(path), because there are three possible scenarios:
- A file’s or directory’s existence is verified (
existsreturnstrueandnotExistsreturnsfalsein this case) - A file’s or directory’s nonexistence is verfied (
existsreturnsfalseandnotExistsreturnstrue) - Neither the existence nor the nonexistence of a file or a directory can be verified (for example due to access restrictions): Both
existsandnonExistsreturn false.
Checking whether a path points to a file or a directory
Section titled “Checking whether a path points to a file or a directory”This is done using Files.isDirectory(Path path) and Files.isRegularFile(Path path)
Path p1 = Paths.get("/var/www");Path p2 = Paths.get("/home/testuser/File.txt");Files.isDirectory(p1) == trueFiles.isRegularFile(p1) == false
Files.isDirectory(p2) == falseFiles.isRegularFile(p2) == trueGetting properties
Section titled “Getting properties”This can be done using the following methods:
Files.isReadable(Path path)Files.isWritable(Path path)Files.isExecutable(Path path)
Files.isHidden(Path path)Files.isSymbolicLink(Path path)Getting MIME type
Section titled “Getting MIME type”Files.probeContentType(Path path)This tries to get the MIME type of a file. It returns a MIME type String, like this:
text/plainfor text filestext/htmlfor HTML pagesapplication/pdffor PDF filesimage/pngfor PNG files
Reading files
Section titled “Reading files”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 parameter (default is StandardCharsets.UTF_8):
List<String> linesOfContent = Files.readAllLines(p2, StandardCharsets.ISO_8859_1);Writing files
Section titled “Writing files”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.write(Path path, byte[] bytes)Existing files wile be overridden, non-existing files will be created.
Syntax
Section titled “Syntax”- Paths.get(String first, String… more) // Creates a Path instance by its String elements
- Paths.get(URI uri) // Creates a Path instance by a URI