Java - File System - Path

Java Conceptuel Diagram


File System - Path in Java.

The path is unique identifier of a file (directory)



In nio, a Path instance represents a file or directory but doesn't access the file system (for instance in order to verify that a particular Path exists)

Path myPath1 = Paths.get("C:\\temp1\\temp2");
// Is Equivalent to
// Path myPath2 = FileSystems.getDefault().getPath("C:\\temp1\\temp2\\temp3");

Meta Information retrieval

  • from a nio path object:
System.out.println("The Name File is: "+myPath1.getFileName());
System.out.println("The number of element in the path is: "+myPath1.getNameCount());
System.out.println("The path of the parent directory is: "+myPath1.getParent());
System.out.println("The root is: "+myPath1.getRoot());
System.out.println("Index retrieval: First element in the path is: "+myPath1.getName(0));
System.out.println("Slice retrieval: Subpath from 0 until 2 is : "+myPath1.subpath(0,2));
The Name File is: temp2
The number of element in the path is: 2
The path of the parent directory is: C:\temp1
The root is: C:\

Index retrieval: First element in the path is: temp1
Slice retrieval: Subpath from 0 until 2 is : temp1\temp2



The path separator used in any search path variable

; # Output on windows


\ # Output on windows

Relativize / Resolve

To relativize a path, you use the relativize function

Path relativePath = basePath.relativize(absolutePath);

The inverse is called resolve

Path absolutePath = basePath.resolve(relativePath);

Discover More
Java Conceptuel Diagram
Java - (Nio|New I/O) Package

The Java NIO model is the last and second IO and File System mechanism of Java. It's available since Java 7. Before, the IO model was based on . In NIO, you define a file or a directory by creating...
Java Conceptuel Diagram
Java - File

in Java In the Java nio model, a file is uniquely identified by a path object. In , file instances represent file or directory names. with the nio library java/io/FileReaderFileReader...

Share this page:
Follow us:
Task Runner