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 in Microsoft Windows). Each directory can contain files and subdirectories, which in turn can contain files and subdirectories, and so on, potentially to an almost limitless depth.
This section covers the following:
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:
. The Solaris OS supports a single root node, which is denoted by the slash character, /
.
Aug 23, 2010 You can use any path separator in Java, it will work on both Unix and Windows. If you still want to use the system path separator there is the File.separator property which will give you the right one depending on the current system. 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 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 in the Solaris OS:
In Microsoft Windows, statusReport
is described by the following notation:
The character used to separate the directory names (also called the delimiter) is specific to the file system: The Solaris OS uses the forward slash (/
), and Microsoft Windows uses the backslash 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 in the file system.
Symbolic Links
File system objects are most typically directories or files. Everyone is familiar with these objects. 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 are when a symbolic link is deleted, or renamed in which case the link itself is deleted, or renamed and not the target of the link.
In the following figure, logFile
appears to be a regular file to the user, but it is actually a symbolic link to dir/logs/HomeLogFile
. HomeLogFile
is the target of the link.
A symbolic link is usually transparent to the user. Reading or writing to a symbolic link is the same as 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 the 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. A circular reference occurs when the target of a link points back to the original link. The circular reference might 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. However, this scenario has been accounted for and will not cause your program to loop infinitely.
The next page discusses the heart of file I/O support in the Java programming language, the Path
class.
A Path
represents a path that is hierarchical and composed of a sequence of directory and file name elements separated by a special separator or delimiter. A root component, that identifies a file system hierarchy, may also be present. The name element that is farthest from the root of the directory hierarchy is the name of a file or directory. The other name elements are directory names. A Path
can represent a root, a root and a sequence of names, or simply one or more name elements. A Path
is considered to be an empty path if it consists solely of one name element that is empty. Accessing a file using an empty path is equivalent to accessing the default directory of the file system. Path
defines the getFileName
, getParent
, getRoot
, and subpath
methods to access the path components or a subsequence of its name elements.
In addition to accessing the components of a path, a Path
also defines the resolve
and resolveSibling
methods to combine paths. The relativize
method that can be used to construct a relative path between two paths. Paths can be compared
, and tested against each other using the startsWith
and endWith
methods.
This interface extends Watchable
interface so that a directory located by a path can be registered
with a WatchService
and entries in the directory watched.
WARNING: This interface is only intended to be implemented by those developing custom file system implementations. Methods may be added to this interface in future releases.
Accessing Files
Paths may be used with the Files
class to operate on files, directories, and other types of files. For example, suppose we want a BufferedReader
to read text from a file 'access.log
'. The file is located in a directory 'logs
' relative to the current working directory and is UTF-8 encoded.
Interoperability
File Path In Mac For Java Tutorial
Paths associated with the default provider
are generally interoperable with the java.io.File
class. Paths created by other providers are unlikely to be interoperable with the abstract path names represented by java.io.File
. The toPath
method may be used to obtain a Path
from the abstract path name represented by a java.io.File
object. The resulting Path
can be used to operate on the same file as the java.io.File
object. In addition, the toFile
method is useful to construct a File
from the String
representation of a Path
.
Concurrency
File Path In Mac For Java Free
Implementations of this interface are immutable and safe for use by multiple concurrent threads.