I'm writing some java code that creates a directory and then puts some files in that directory. I don't care about the order in which the files are created, however, for the files to be successfully created the directory needs to exit first.
I want to use the (newish) java.nio.Files api as it has quite an elegant set of methods for working with files and directories.
While it's nice that the Files
class is non-blocking it means that the directory doesn't always exist. Is there a "right" way to determine when the Files.createDirectories(path)
has completed?
Not particularly complex (which is why I like the Files api) but here's some sample code:
Files.createDirectories(path);
Files.write(filename1, "some content".getBytes());
Files.write(filename2, "some other content".getBytes());
In the above path
might be /tmp/blah
and filename1
= /tmp/blah/foo
and filename2
= /tmp/blah/bar
.
Obviously I can do this using some other class or library (like Commons IO) but the build in java.nio.Files is quite nice.
I think you misunderstood. Those operations are actually synchronous, so you're good to go here. Files.write
uses a java.io.OutputStream
for example.
Some Name addressed the possible implementation of createDirectory
for Linux.
On Windows, createDirectory
uses the CreateDirectory WinAPI function.
WindowsNativeDispatcher.CreateDirectory(var3.getPathForWin32Calls(), var4.address() /* Security descriptor */);
Buffers
, Channels
and Selectors
are non-blocking.