Trail: Essential Classes
Lesson: Basic I/O
Section: File I/O (featuring NIO.2)
What is a Path? (And Other File System Facts)
Home Page > Essential Classes > Basic I/O
What is a Path? (And Other File System Facts)
A file system stores and organizes files on some form of media, generally one or more hard drives, in such a way that they can be easily retrieved. Most file systems in use today store the files in a tree (or hierarchical) structure. At the top of the tree is one (or more) root nodes. Under the root node, there are files and directories (folders on Microsoft Windows). Each directory may contain files and subdirectories, which in turn may contain files and subdirectories, and so on — potentially to an almost limitless depth.

This section covers:

What is a Path?

The following figure shows a sample directory tree containing a single root node. Microsoft Windows supports multiple root nodes — each root node maps to a volume, such as C:\ or D:\. Solaris supports a single root node, which is denoted by the slash character, /.

Sample directory structure

Sample Directory Structure.

A file is identified by its path through the file system, beginning from the root node. For example, the statusReport file in the previous figure is described by the following notation on Solaris:
/home/sally/statusReport
On Microsoft Windows, statusReport is described by the following notation:
C:\home\sally\statusReport
As you can see, the character used to separate the directory names (also called the delimiter) is specific to the file system: Solaris uses slash (/) and Microsoft Windows uses forward slash (\).

Relative or Absolute?

A path is either relative or absolute. An absolute path always contains the root element and the complete directory list required to locate the file; for example: /home/sally/statusReport is an absolute path. All of the information needed to locate the file is contained in the path string.

A relative path needs to be combined with another path in order to access a file. For example, joe/foo is a relative path. Without more information, a program cannot reliably locate the joe/foo directory on the file system.

Symbolic Links

File system objects are most typically directories or files — everyone is familiar with these. But some file systems also support the notion of symbolic links. (A symbolic link is also referred to as a symlink or a soft link).

A symbolic link is a special file that serves as a reference to another file. For the most part, symbolic links are transparent to applications and operations on symbolic links are automatically redirected to the target of the link. (The file or directory being pointed to is called the target of the link.) Exceptions to this are when a symbolic link is deleted, removed or renamed in which case the link itself is deleted, removed or renamed and not the target of the link.

In the following figure, logFile appears to be a normal file to the user, but it is actually a symbolic link to dir/logs/HomeLogfile. HomeLogfile is the target of the link.

Sample symbolic link

Example of a Symbolic Link.

A symbolic link is usually transparent to the user — reading or writing to a symbolic link works like reading or writing to any other file or directory.

The phrase resolving a link means to substitute the actual location in the file system for the symbolic link. In our example, resolving logFile yields dir/logs/HomeLogFile.

In real-world scenarios, most file systems make liberal use of symbolic links. Occasionally, a carelessly created symbolic link can cause a circular reference: this occurs when the target of a link points back to the original link. The circular reference may be indirect — directory a points to directory b which points to directory c which contains a subdirectory pointing back to directory a. Circular references can cause havoc when a program is recursively walking a directory structure. As you will see, this scenario has been accounted for and will not cause your program to loop infinitely.

The next section looks at the heart of file I/O support in Java, the Path class.

Previous page: File I/O (featuring NIO.2)
Next page: The Path Class