Getting Filesystem Paths in Java
Learn the difference between path, absolute and canonical paths. Also, learn to get the path of a file in Java using standard IO and New IO classes.
1. Relative, Absolute and Canonical Paths
A relative path is a path that describes the location of a file or folder in relative to the current working directory. Unlike absolute paths, relative paths contain information that is only relative to the current directory.
This is the path, which we generally provide in the File class’s constructor.
src\main\java\com\howtodoinjava\io\foo\foo.txt src\main\java\com\howtodoinjava\io\foo\bar\bar.txt
An absolute path always contains the root element and the complete directory hierarchy required to locate the file. There is no more information required further to access the file or path.
- Absolute paths contain all the relevant information to find the resources indicated by an absolute URL.
- Absolute paths may contain the shorthands like single and double dots in the file paths.
C:\Users\lokesh\IdeaProjects\Core-Java\src\main\java\com\howtodoinjava\io\foo\foo.txt C:\Users\lokesh\IdeaProjects\Core-Java\src\main\java\com\howtodoinjava\io\foo\bar\..\foo.txt
Canonical paths are absolute paths after resolving the shorthands or redundant names like dots “ . ” and “ .. ” as per the directory structure.
The canonical path of a file just “purifies” the path to the absolute path, removing and resolving stuff like dots and resolving symlinks (on UNIX).
C:\Users\lokesh\IdeaProjects\Core-Java\src\main\java\com\howtodoinjava\io\foo\foo.txt C:\Users\lokesh\IdeaProjects\Core-Java\src\main\java\com\howtodoinjava\io\foo\bar\bar.txt
2. Get File Paths Using java.io.File
The java.io.File class has three methods to obtain the filesystem paths:
- file.getPath() : returns the same string passed as the File constructor.
- file.getAbsolutePath() : returns the pathname of the file after resolving the path for the current user’s directory. It does not resolve the shorthands like single and double dots.
- file.getCanonicalPath() : returns the full path after resolving the absolute pathname as well as the shorthands.
File file = new File("src/main/java/com/howtodoinjava/io/foo/bar/../foo.txt"); String givenPath = file.getPath(); String absPath = file.getAbsolutePath(); String canPath = file.getCanonicalPath(); System.out.println(givenPath); System.out.println(absPath); System.out.println(canPath);
src\main\java\com\howtodoinjava\io\foo\bar\..\foo.txt C:\Users\lokes\IdeaProjects\Core-Java\src\main\java\com\howtodoinjava\io\foo\bar\..\foo.txt C:\Users\lokes\IdeaProjects\Core-Java\src\main\java\com\howtodoinjava\io\foo\foo.txt
3. Get File Paths Using java.nio.file.Path
The java.nio.file.Path also provides following methods for getting various paths:
- path.toAbsolutePath() – Full file path similar to file.getAbsolutePath().
- path.toRealPath() – Canonical path similar to file.getCanonicalPath().
The Path methods throws NoSuchFileException if the file is not present in the specified location. The File methods do not throw any exceptions.
Path path = Path.of("src/main/java/com/howtodoinjava/io/foo/bar/../foo.txt"); givenPath = path.toString(); absPath = path.toAbsolutePath().toString(); canPath = path.toRealPath().toString(); System.out.println(givenPath); System.out.println(absPath); System.out.println(canPath);
src\main\java\com\howtodoinjava\io\foo\bar\..\foo.txt C:\Users\lokes\IdeaProjects\Core-Java\src\main\java\com\howtodoinjava\io\foo\bar\..\foo.txt C:\Users\lokes\IdeaProjects\Core-Java\src\main\java\com\howtodoinjava\io\foo\foo.txt
In this tutorial, we understood different kinds of filesystem paths in Java and how to get these paths using Java APIs.
As a best practice, always use the canonical paths if we are not sure if the provided path contains the shorthand characters or not.
Java io file getabsolutepath
The conversion of a pathname string to or from an abstract pathname is inherently system-dependent. When an abstract pathname is converted into a pathname string, each name is separated from the next by a single copy of the default separator character. The default name-separator character is defined by the system property file.separator , and is made available in the public static fields separator and separatorChar of this class. When a pathname string is converted into an abstract pathname, the names within it may be separated by the default name-separator character or by any other name-separator character that is supported by the underlying system.
A pathname, whether abstract or in string form, may be either absolute or relative. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes. A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir , and is typically the directory in which the Java virtual machine was invoked.
The parent of an abstract pathname may be obtained by invoking the getParent() method of this class and consists of the pathname’s prefix and each name in the pathname’s name sequence except for the last. Each directory’s absolute pathname is an ancestor of any File object with an absolute abstract pathname which begins with the directory’s absolute pathname. For example, the directory denoted by the abstract pathname «/usr» is an ancestor of the directory denoted by the pathname «/usr/local/bin» .
- For UNIX platforms, the prefix of an absolute pathname is always «/» . Relative pathnames have no prefix. The abstract pathname denoting the root directory has the prefix «/» and an empty name sequence.
- For Microsoft Windows platforms, the prefix of a pathname that contains a drive specifier consists of the drive letter followed by «:» and possibly followed by «\\» if the pathname is absolute. The prefix of a UNC pathname is «\\\\» ; the hostname and the share name are the first two names in the name sequence. A relative pathname that does not specify a drive has no prefix.
Instances of this class may or may not denote an actual file-system object such as a file or a directory. If it does denote such an object then that object resides in a partition. A partition is an operating system-specific portion of storage for a file system. A single storage device (e.g. a physical disk-drive, flash memory, CD-ROM) may contain multiple partitions. The object, if any, will reside on the partition named by some ancestor of the absolute form of this pathname.
A file system may implement restrictions to certain operations on the actual file-system object, such as reading, writing, and executing. These restrictions are collectively known as access permissions. The file system may have multiple sets of access permissions on a single object. For example, one set may apply to the object’s owner, and another may apply to all other users. The access permissions on an object may cause some methods in this class to fail.
Instances of the File class are immutable; that is, once created, the abstract pathname represented by a File object will never change.
Interoperability with java.nio.file package
The java.nio.file package defines interfaces and classes for the Java virtual machine to access files, file attributes, and file systems. This API may be used to overcome many of the limitations of the java.io.File class. The toPath method may be used to obtain a Path that uses the abstract path represented by a File object to locate a file. The resulting Path may be used with the Files class to provide more efficient and extensive access to additional file operations, file attributes, and I/O exceptions to help diagnose errors when an operation on a file fails.
Class File
The conversion of a pathname string to or from an abstract pathname is inherently system-dependent. When an abstract pathname is converted into a pathname string, each name is separated from the next by a single copy of the default separator character. The default name-separator character is defined by the system property file.separator , and is made available in the public static fields separator and separatorChar of this class. When a pathname string is converted into an abstract pathname, the names within it may be separated by the default name-separator character or by any other name-separator character that is supported by the underlying system.
A pathname, whether abstract or in string form, may be either absolute or relative. An absolute pathname is complete in that no other information is required in order to locate the file that it denotes. A relative pathname, in contrast, must be interpreted in terms of information taken from some other pathname. By default the classes in the java.io package always resolve relative pathnames against the current user directory. This directory is named by the system property user.dir , and is typically the directory in which the Java virtual machine was invoked.
The parent of an abstract pathname may be obtained by invoking the getParent() method of this class and consists of the pathname’s prefix and each name in the pathname’s name sequence except for the last. Each directory’s absolute pathname is an ancestor of any File object with an absolute abstract pathname which begins with the directory’s absolute pathname. For example, the directory denoted by the abstract pathname «/usr» is an ancestor of the directory denoted by the pathname «/usr/local/bin» .
- For UNIX platforms, the prefix of an absolute pathname is always «/» . Relative pathnames have no prefix. The abstract pathname denoting the root directory has the prefix «/» and an empty name sequence.
- For Microsoft Windows platforms, the prefix of a pathname that contains a drive specifier consists of the drive letter followed by «:» and possibly followed by «\\» if the pathname is absolute. The prefix of a UNC pathname is «\\\\» ; the hostname and the share name are the first two names in the name sequence. A relative pathname that does not specify a drive has no prefix.
Instances of this class may or may not denote an actual file-system object such as a file or a directory. If it does denote such an object then that object resides in a partition. A partition is an operating system-specific portion of storage for a file system. A single storage device (e.g. a physical disk-drive, flash memory, CD-ROM) may contain multiple partitions. The object, if any, will reside on the partition named by some ancestor of the absolute form of this pathname.
A file system may implement restrictions to certain operations on the actual file-system object, such as reading, writing, and executing. These restrictions are collectively known as access permissions. The file system may have multiple sets of access permissions on a single object. For example, one set may apply to the object’s owner, and another may apply to all other users. The access permissions on an object may cause some methods in this class to fail.
Instances of the File class are immutable; that is, once created, the abstract pathname represented by a File object will never change.
Interoperability with java.nio.file package
The java.nio.file package defines interfaces and classes for the Java virtual machine to access files, file attributes, and file systems. This API may be used to overcome many of the limitations of the java.io.File class. The toPath method may be used to obtain a Path that uses the abstract path represented by a File object to locate a file. The resulting Path may be used with the Files class to provide more efficient and extensive access to additional file operations, file attributes, and I/O exceptions to help diagnose errors when an operation on a file fails.