NIO 2 File API: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
Line 13: 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>


Create one directory:
Create one directory:

Revision as of 01:32, 2 April 2021

External

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


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