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.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 be removed
// when we are working on the current directory.
Path path = Paths.get("./README.md");
System.out.println("Path = " + path);
// Removes redundant name elements from the path.
path = path.normalize();
System.out.println("Path = " + path);
}
}
Latest posts by Wayan (see all)
- How do I get the number of processors available to the JVM? - March 29, 2023
- How do I show Spring transaction in log / console? - March 29, 2023
- How do I build simple search page using ZK and Spring Boot? - March 8, 2023