In this tutorial, we will understand the basic concepts of abstract, absolute, relative, and canonical pathnames in java with suitable examples.
What are Path and Pathname?
A path is a hierarchy of directories that locate a file or a directory.
A pathname is a string representation of a path. A separator character (such as Windows backslash [\] ) that is platform-dependent is present between the consecutive names.
For example, suppose we create two objects of the File class that stores two pathname strings. The statements are:
1) File file = new File("/Java/myfile.dat"); // For Unix or Linux platform. 2) File file = new File("D:\\Java\\myfile.dat"); // For Windows platform.
In the first statement, a pathname starts with the root directory symbol /, and continues with directory name Java, separator character /, file name myfile.dat.
In the second statement, a pathname starts with drive specifier D:, and continues with root directory symbol \, directory name Java, separator character \, and filename myfile.dat.
Abstract Pathname in Java
When a file or directory is stored in a system, the pathname string that represents the name is machine-dependent.
An abstract pathname consists of an optional prefix string, such as disk drive specifiers, “/” for Unix, or “\\” for Windows, and a sequence of zero or more string names.
The prefix string is platform-dependent. The last name in the abstract pathname represents a file or directory. All other names represent directories.
When an abstract pathname is converted into a pathname string, each name is separated from the next name by the default name separator character.
For example, File(String pathname) converts pathname string /Java/myfile.dat to abstract pathname \Java\myfile.dat on a Windows platform and /Java/myfile.dat on the Linux platform.
Absolute Pathname in Java
An absolute pathname is a pathname that starts with a root directory symbol. It is considered a complete name. Complete means no other information is required to locate a file.
For example, suppose the file ScienceDemo.java is placed at the following location:
This complete name is the absolute pathname of the file ScienceDemo.java. No other information is required to locate this file in the system.
Relative Pathname in Java
A relative pathname is a pathname that does not start with the root directory symbol. It is interpreted through information taken from some other pathname.
For example, JavaProject\src\JavaProgram\ScienceDemo.java is a relative pathname. JavaProject is the current user directory.
The current directory can be determined by the following syntax:
Canonical Pathname in Java
A canonical pathname is a pathname that is considered both absolute and unique. This pathname is system-dependent.
For example, suppose an absolute path of a file on Microsoft Windows machine is
Consider the following statements below:
// Creating an object of File class. File file = new File(d:\\Java\\workspace\\JavaProject\\src\\JavaProgram\\ScienceDemo.java); System.out.println(f.getAbsolutePath()); System.out.println(f.getCanonicalPath());
The call to getAbsolutePath() method will display the following string:
The call to getCanonicalPath() method will display the following string:
As you can observe in the canonical path, the drive letter is converted to upper case on a Microsoft Windows system.
Hope that this tutorial has covered almost all the important points related to abstract, absolute, relative, and canonical pathnames in Java. I hope that you will have understood the basic points of path and pathnames.
Thanks for reading!!!