Java: Interacting with the File System

Many applications in Java rely on computer memory to perform tasks. Computer memory is a kind of temporary storage. When you turn the computer off, the memory is gone. However, another sort of memory exists — files on disk. These files are more permanent in nature.

If you don’t specifically delete a file (or the system deletes it for you), the file continues to exist on disk (with the exception of some major calamity such as a disk crash). The permanent memory provided by files lets applications pick up where they left off between computer sessions. Files also hold the data you want to work with in some way.

You can discover techniques used to locate, read, and write files on the hard drive. By knowing these techniques, you can move forward in learning how to locate, read, and write files from other data sources, such as the network. Working with files is a first step into entering a new world of data.

It doesn’t matter whether you’re using a Macintosh, Linux, or Windows computer — every operating system has some sort of file system associated with it. Yes, differences between file systems exist, but even so, the basic concepts are the same. A file is stored in a specific location on the drive, and if you know that location, you can read and write the file.

  • A file is a storage container for data. Files can use a number of methods to hold and organize the data. Some of these organizational methods are easily readable by humans (such as text files), but others aren’t (such as graphics files).

  • A directory is a storage container for both directories (called subdirectories) and files. The directory is associated with the operating system’s file structure. It’s usually hierarchical.

  • A path is a description of the location of a particular directory or file on the hard drive. An absolute path starts with the drive and the root node of the drive and then works its way down to the specific directory or file.

    Most operating systems also support relative paths that define the location of a directory or file based on the current location in the directory hierarchy.

Most operating systems rely on a hierarchical storage structure that relies on directories of files. Essentially, a directory is simply a container used to store files and other directories.

The location of the directory on disk is called the path. A path normally begins at the uppermost point of the drive hierarchy, called the root node in most operating systems, and works its way down. For example, a path of C:\ would be the root node on the C drive. The Temp directory located within the root node would have a path of C:\Temp.

Some operating systems allow the use of relative paths. A relative path describes the location of a directory or file based on the current location. Using relative paths can be tricky and won’t be explored in this chapter.

A final path designation is the current path — the current location within the hierarchy. Most Java applications rely on the application directory as the current path (sometimes also called the default path).

Many Java applications rely on the current location in the interest of simplicity. In addition, using the current location means that the applications will work equally well on Linux, Macintosh, and Windows systems.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement

Inside Dummies.com