Java Reference
In-Depth Information
Accessing Files and Directories
In this chapter, we will explore how we identify, access, and manipulate files and directories on your
hard drive. This will include the ability to create new files and directories, but not to read or write files.
We will get to that starting in the next chapter.
In this chapter you will learn:
How you create
File
objects and use them to examine files and directories
❑
How you can use
File
class methods to examine the contents of the hard drives on your system
❑
How to create new files and directories on your hard drive
❑
How to create temporary files
❑
How you create
FileOutputStream
objects
❑
Working with File Objects
It is easy to forget that a
File
object doesn't actually represent a file. You need to keep reminding
yourself that a
File
object encapsulates a
pathname
or
reference
to what may or may not be a physical
file or directory on your hard disk, not the physical file or directory itself. The fact that you create a
File
object does not determine that a file or directory actually exists. This is not as strange as it might
seem at first sight. After all, you will often be defining a
File
object that encapsulates a path to a new
file or directory that you intend to create.
As we will see, a
File
object serves two purposes:
It enables you to check the pathname that it encapsulates against the physical file system and
see whether it corresponds to a real file or directory.
❑
You can use it to create file stream objects.
❑