Java Reference
In-Depth Information
I/O. These topics describe all Java I/O classes one by one. This chapter looks at Java I/O from a different perspective.
First, you will look at the design pattern that was used to design the Java I/O classes. Once you understand the design
pattern behind it, it is easy to understand how to use those classes to perform I/O in your program. After all, I/O is all
about reading and writing data and it should not be that hard to understand! Before you start looking at the design
pattern for the I/O classes, you will learn how to deal with a file in the next section.
Working with Files
How do you refer to a file in your computer? You refer to it by its pathname. A file's pathname is a sequence of
characters by which you can identify it uniquely in a file system. A pathname consists of a file name and its unique
location in the file system. For example, on a Windows platform, C:\users\dummy.txt is the pathname for a file
named dummy.txt, which is located in the directory named users , which in turn is located in the root directory in the
C: drive. On a UNIX platform, /users/dummy is the pathname for a file named dummy, which is located in the directory
named users , which in turn is located in the root directory.
A pathname can be either absolute or relative. An absolute pathname points to the same location in a file system
irrespective of the current working directory. For example, on a Windows platform, C:\users\dummy.txt is an
absolute pathname.
A relative pathname is resolved with respect to the working directory. Suppose dummy.txt is your pathname. If
your working directory is C:\ , this pathname points to C:\dummy.txt . If your working directory is C:\users , it points
to C:\users\dummy.txt . Note that if you specify a relative pathname for a file, it points to a different file depending on
the current working directory. A pathname that starts with a root is an absolute pathname. The forward slash ( / ) is the
root on the UNIX platform and a drive letter such as A: or C: defines the root for the Windows platform.
the pathname syntax is platform-dependent. programs using any platform-dependent syntax to represent
pathnames may not work correctly on other platforms. In this chapter, most of the time I use the term “file” to mean a file
or a directory.
Tip
Creating a File Object
An object of the File class is an abstract representation of a pathname of a file or a directory in a platform-
independent manner. You can create a File object from
A pathname
A parent pathname and a child pathname
Use one of the following constructors of the File class to create a file:
A URI (uniform resource identifier)
File(String pathname)
File(File parent, String child)
File(String parent, String child)
If you have a file pathname string of dummy.txt , you can create an abstract pathname (or a File object), like so:
File(URI uri)
 
 
Search WWH ::




Custom Search