To eliminate redundant elements from a Path
we can use the Path.normalize()
method. For example in the following code snippet. When try accessing the README
file in the current directory the .
symbol in the Path
elements considered to be redundant, we don’t need it. That’s why we normalize the Path
.
package org.kodejava.example.io;
import java.nio.file.Path;
import java.nio.file.Paths;
public class PathNormalize {
public static void main(String[] args) {
// The following Path contains a redundant element. The "." which
// basically point to the current directory can simply removed when
// we are working on the current directory.
Path path = Paths.get("./README");
System.out.println("Path = " + path);
// Removes redundant name elements from the path.
path = path.normalize();
System.out.println("Path = " + path);
}
}
Wayan Saryada
Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳
Latest posts by Wayan Saryada (see all)
- How do I set the time of java.util.Date instance to 00:00:00? - October 24, 2019
- How to Install Consolas Font in Mac OS X? - March 29, 2019
- How do I clear the current command line in terminal? - February 14, 2019