NIO 2 File API: Difference between revisions
Jump to navigation
Jump to search
(10 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
=External= | =External= | ||
* https://gquintana.github.io/2017/09/02/Java-File-vs-Path.html | * https://gquintana.github.io/2017/09/02/Java-File-vs-Path.html | ||
* https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html | |||
* https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Paths.html | |||
=Internal= | =Internal= | ||
Line 11: | Line 13: | ||
=Recipes= | =Recipes= | ||
Build a new absolute Path instances from components: | |||
<syntaxhighlight lang='java'> | |||
Path p = Paths.get("/Users", "ovidiu", "tmp") | |||
</syntaxhighlight> | |||
Build a new relative Path instances from components: | |||
<syntaxhighlight lang='java'> | |||
Path p = Paths.get("ovidiu", "tmp") | |||
</syntaxhighlight> | |||
<font color=darkgray> | |||
To research: | |||
<syntaxhighlight lang='java'> | |||
Path.of("") | |||
</syntaxhighlight> | |||
</font> | |||
Create one directory: | Create one directory: | ||
<syntaxhighlight lang='java'> | <syntaxhighlight lang='java'> | ||
Path parent = ...; | |||
Path dir = Files.createDirectory(Paths.get(parent.toString(), "my-dir")); | |||
</syntaxhighlight> | </syntaxhighlight> | ||
Create a directory hierarchy: | Create a directory hierarchy: | ||
<syntaxhighlight lang='java'> | <syntaxhighlight lang='java'> | ||
Path parent = ...; | |||
Path dir = Files.createDirectories(Paths.get(parent.toString(), "dir1/dir2/dir3")); | |||
</syntaxhighlight> | |||
Get file name: | |||
<syntaxhighlight lang='java'> | |||
Path file = ...; | |||
String fileName = file.getFileName().toString(); | |||
</syntaxhighlight> | |||
Create a file with a specified content: | |||
<syntaxhighlight lang='java'> | |||
Path file = ...; | |||
Files.write(file, "something".getBytes()); | |||
</syntaxhighlight> | |||
Verify that a directory exists (works for both absolute and relative paths) | |||
<syntaxhighlight lang='java'> | |||
Path p = Paths.get("/Users/ovidiu/tmp"); | |||
assert Files.isDirectory(p) == true | |||
// run from "/Users" | |||
Path p = Paths.get("ovidiu/tmp"); | |||
assert Files.isDirectory(p) == true | |||
</syntaxhighlight> | |||
Get current directory: | |||
<syntaxhighlight lang='java'> | |||
new File(".").toPath().toAbsolutePath().toString() | |||
</syntaxhighlight> | </syntaxhighlight> |
Latest revision as of 06:23, 2 April 2021
External
- https://gquintana.github.io/2017/09/02/Java-File-vs-Path.html
- https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Files.html
- https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/nio/file/Paths.html
Internal
Subjects
Recipes
Build a new absolute Path instances from components:
Path p = Paths.get("/Users", "ovidiu", "tmp")
Build a new relative Path instances from components:
Path p = Paths.get("ovidiu", "tmp")
To research:
Path.of("")
Create one directory:
Path parent = ...;
Path dir = Files.createDirectory(Paths.get(parent.toString(), "my-dir"));
Create a directory hierarchy:
Path parent = ...;
Path dir = Files.createDirectories(Paths.get(parent.toString(), "dir1/dir2/dir3"));
Get file name:
Path file = ...;
String fileName = file.getFileName().toString();
Create a file with a specified content:
Path file = ...;
Files.write(file, "something".getBytes());
Verify that a directory exists (works for both absolute and relative paths)
Path p = Paths.get("/Users/ovidiu/tmp");
assert Files.isDirectory(p) == true
// run from "/Users"
Path p = Paths.get("ovidiu/tmp");
assert Files.isDirectory(p) == true
Get current directory:
new File(".").toPath().toAbsolutePath().toString()