Java Reference
In-Depth Information
Note that the Java NIO2 API also provides a different method to read and write files, named
channel I/O, as an alternative to stream-based I/O. Whereas streams read one character or byte
at a time, channels can read or write buffers at a time (and thus avoid the use of a separate buffer-
ing mechanism). In addition, channels exist that allow for more fine-grained seeking within a file
through a concept called Random Access Files, which permit non-sequential access to files and
which allow you to map the contents of a file directly to computer memory. We mention it here for
the sake of completeness, but in most “normal” file I/O environments, stream-based I/O works
just fine.
Lastly, we can take a look at the methods to create files. While you might just use one of the earlier
writing functions to open a new file and immediately close it, it is much cleaner to use the Files.
createFile(Path path) method to create an empty file. Note that this method will—by default—
throw a permission if a path exists, contrary to the writing methods shown previously, which will
overwrite a file if it exists. It is thus a good idea to use this method as an extra fail-safe in cases
where this matters. You can also use another method— Files.createTempFile(Path folder,
String prefix, String suffix —to create temporary files in the specified folder, using a given
prefix, suffix, and a randomized body as a name. Note that you can leave the folder argument out,
in which case the standard temporary-file directory provided by your operating system will be used.
These methods are helpful to create quick “throwaway” files.
Reading and Creating Folders
Some path operation methods, such as deletion or copying, can work on files and folders. But direc-
tories also require an additional set of methods. For example, how would you list all the contents of
a folder?
First of all, though, let's quickly take a look at creating folders. This can be done using the Files.
createDirectory(Path path) method or the createTempDirectory(Path folder, String
prefix) method in case you want to create a temporary folder (again, the folder argument can
be left out).
You list all the contents of a folder using the Files.newDirectoryStream(Path folder) method.
Note that this method returns an object that implements the DirectoryStream and Iterable
interfaces, so you can loop over this object to read all of the entries. However, since this object
is a stream, don't forget to close it in your finally block (or use a try-with-resources block as
we've been recommending so far). The following example class shows how to list the contents of a
directory:
import java.io.IOException;
import java.nio.file.DirectoryIteratorException;
import java.nio.file.DirectoryStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class ShowDirectory {
public static void main(String[] args) {
Path folder = Paths.get("C:\\");
try (DirectoryStream<Path> stream = Files.newDirectoryStream(folder)) {
Search WWH ::




Custom Search