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 number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024