Java Reference
In-Depth Information
• Java also can associate streams with different devices. Three stream objects are associated with
devices when a Java program begins executing—
System.in
,
System.out
and
System.err
.
Section 15.3 Using NIO Classes and Interfaces to Get File and Directory Information
•A
Path
(p. 647) represents the location of a file or directory. Path objects do not open files or
provide any file-processing capabilities.
• Class
Paths
(p. 647) is used to get a
Path
object representing a file or directory location.
• Class
Files
(p. 647) provides
static
methods for common file and directory manipulations, in-
cluding methods for copying files; creating and deleting files and directories; getting information
about files and directories; reading the contents of files; getting objects that allow you to manip-
ulate the contents of files and directories; and more.
•A
DirectoryStream
(p. 647) enables a program to iterate through the contents of a directory.
•The
static
method
get
(p. 647) of class
Paths
converts a
String
representing a file's or directo-
ry's location into a
Path
object.
• Character-based input and output can be performed with classes
Scanner
and
Formatter
.
• Class
Formatter
(p. 647) enables formatted data to be output to the screen or to a file in a man-
ner similar to
System.out.printf
.
• An absolute path (p. 647) contains all the directories, starting with the root directory (p. 647),
that lead to a specific file or directory. Every file or directory on a disk drive has the same root
directory in its path.
• A relative path (p. 647) starts from the directory in which the application began executing.
•
Files
static
method
exists
(p. 648) receives a
Path
and determines whether it exists (either as
a file or as a directory) on disk.
•
Path
method
getFileName
(p. 648) gets the
String
name of a file or directory without any loca-
tion information.
•
Files
static
method
isDirectory
(p. 648) receives a
Path
and returns a
boolean
indicating
whether that
Path
represents a directory on disk.
•
Path
method
isAbsolute
(p. 648) returns a
boolean
indicating whether a
Path
represents an ab-
solute path to a file or directory.
•
Files
static
method
getLastModifiedTime
(p. 648) receives a
Path
and returns a
FileTime
(package
java.nio.file.attribute
) indicating when the file was last modified.
•
Files
static
method
size
(p. 648) receives a
Path
and returns a
long
representing the number
of bytes in the file or directory. For directories, the value returned is platform specific.
•
Path
method
toString
(p. 648) returns a
String
representation of the
Path
.
•
Path
method
toAbsolutePath
(p. 648) converts the
Path
on which it's called to an absolute path.
•
Files
static
method
newDirectoryStream
(p. 648) returns a
DirectoryStream<Path>
contain-
ing
Path
objects for a directory's contents.
• A separator character (p. 650) is used to separate directories and files in the path.
Section 15.4 Sequential-Access Text Files
• Java imposes no structure on a file. You must structure files to meet your application's needs.
• To retrieve data sequentially from a file, programs normally start from the beginning of the file
and read all the data consecutively until the desired information is found.
• Data in many sequential files cannot be modified without the risk of destroying other data in the
file. Records in a sequential-access file are usually updated by rewriting the entire file.