Saturday, June 13, 2015

Example of Path class in Java NIO

The Path class, introduced in the Java SE 7 release, is one of the primary entrypoints of the java.nio.file package. If your application uses file I/O, you will want to learn about the powerful features of this class.

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.

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, /.
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 in the Solaris OS:

/home/sally/statusReport

In Microsoft Windows, statusReport is described by the following notation:
 
C:\home\sally\statusReport

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.

For More details on Path class or detailed tutorial Visit This Page

This post is not a tutorial but rather I have written down a code which uses Path and shows the output of its different methods. Here's the code :- 


Output

Returns the absolute path :-/home/psycho_coder/NetBeansProjects/Miscel/src/data/en
Returns Real Path with Sym Links removed :/home/psycho_coder/NetBeansProjects/Miscel/src/data/en
Returns the normalized path /home/psycho_coder/NetBeansProjects/Miscel/src/data/en
Returns the parent directory in which the file is present :/home/psycho_coder/NetBeansProjects/Miscel/src/data
Returns the root directory :/
Returns the file name :en
Number of Name elements in the path 7
Resolves the given path against this path's parent path:/home/psycho_coder/NetBeansProjects/Miscel/src/data/en
Returns a subpath of the main path :Miscel/src/data/en
Returns the File path in a URL format : file:///home/psycho_coder/NetBeansProjects/Miscel/src/data/en

0 comments :

Post a Comment

Follow Me!

Blog Archive

Followers

Visitor Map