Directories and Paths

Modern operating systems organize files into hierarchical directories. Each directory contains zero or more files or other directories. Like files, directories have names and attributes, though—depending on the operating system—those names and attributes may be different from the attributes allowed for files. For example on the Macintosh, a file or directory name can be up to 31 bytes long, but a volume name can be no more than 27 bytes long.

Paths and Separators

To specify a file completely, you don’t just give its name. You also give the directory the file lives in. Of course, that directory may itself be inside another directory, which may be in another directory, until you reach the root of the filesystem. The complete list of directories from the root to a specified file plus the name of the file itself is called the absolute path to the file. The exact syntax of absolute paths varies from system to system. Here are a few examples:

DOS

C:\PUBLIC\HTML\JAVAFAQ\INDEX.HTM

Win32

C:\public\html\javafaq\index.html

MacOS

Macintosh HD:public:html:javafaq:index.html

Unix

/public/html/javafaq/index.html

All three strings reference a file named index.html on the primary hard drive in the javafaq directory, which is itself in the html directory, which is in the public directory. One obvious difference is the file separator character. Unix uses a forward slash (/) to separate directories; DOS-based filesystems, including the variants of Windows and ...

Get Java I/O now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.