Java Reference
In-Depth Information
Character-based input and output can be performed with classes Scanner and For-
matter , as you'll see in Section 15.4. You've used class Scanner extensively to input data
from the keyboard. Scanner also can read data from a file. Class Formatter enables for-
matted data to be output to any text-based stream in a manner similar to method
System.out.printf . Appendix I presents the details of formatted output with printf . All
these features can be used to format text files as well. In Chapter 28, we use stream classes
to implement networking applications.
Java SE 8 Adds Another Type of Stream
Chapter 17, Java SE 8 Lambdas and Streams, introduces a new type of stream that's used
to process collections of elements (like arrays and ArrayList s), rather than the streams of
bytes we discuss in this chapter's file-processing examples.
15.3 Using NIO Classes and Interfaces to Get File and
Directory Information
Interfaces Path and DirectoryStream and classes Paths and Files (all from package ja-
va.nio.file ) are useful for retrieving information about files and directories on disk:
Path interface—Objects of classes that implement this interface represent the lo-
cation of a file or directory. Path objects do not open files or provide any file-pro-
cessing capabilities.
Paths class—Provides static methods used to get a Path object representing a
file or directory location.
Files class—Provides static methods for common file and directory manipu-
lations, such as copying files; creating and deleting files and directories; getting
information about files and directories; reading the contents of files; getting ob-
jects that allow you to manipulate the contents of files and directories; and more
DirectoryStream interface—Objects of classes that implement this interface en-
able a program to iterate through the contents of a directory.
Creating Path Objects
You'll use class static method get of class Paths to convert a String representing a file's
or directory's location into a Path object. You can then use the methods of interface Path
and class Files to determine information about the specified file or directory. We discuss
several such methods momentarily. For complete lists of their methods, visit:
http://docs.oracle.com/javase/7/docs/api/java/nio/file/Path.html
http://docs.oracle.com/javase/7/docs/api/java/nio/file/Files.html
Absolute vs. Relative Paths
A file or directory's path specifies its location on disk. The path includes some or all of the
directories leading to the file or directory. An absolute path contains all directories, start-
ing with the root directory , that lead to a specific file or directory. Every file or directory
on a particular disk drive has the same root directory in its path. A relative path is “relative”
to another directory—for example, a path relative to the directory in which the application
began executing.
 
 
Search WWH ::




Custom Search