Java Path normalize

Description

Java Path normalize

import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) {
    Path path = Paths.get("/home/docs/../source/Java/Test.java");
    System.out.println("Absolute path: " + path.toAbsolutePath());
    System.out.println("URI: " + path.toUri());
    System.out.println("Normalized Path: " + path.normalize());
    System.out.println("Normalized URI: " + path.normalize().toUri());
    System.out.println();/*from www  .ja  va 2s  .  c  o m*/

    path = Paths.get("/home/./test/../../Hello World.java");
    System.out.println("Absolute path: " + path.toAbsolutePath());
    System.out.println("URI: " + path.toUri());
    System.out.println("Normalized Path: " + path.normalize());
    System.out.println("Normalized URI: " + path.normalize().toUri());
  }
}
import java.nio.file.Path;
import java.nio.file.Paths;

public class Main {
  public static void main(String[] args) {
    Path p1 = Paths.get("C:\\myData\\def..\\..\\Main.java");
    Path p2 = Paths.get("C:\\MyData\\main.java");

    Path p3 = Paths.get("C:\\myData/../\\..\\myData\\Main.java");

    Path p1n = p1.normalize();//  w  w  w . j a  v a  2 s .com
    System.out.println(p1 + " normalized to " + p1n);

    Path p2n = p2.normalize();
    System.out.println(p2 + " normalized to " + p2n);

    Path p3n = p3.normalize();
    System.out.println(p3 + " normalized to " + p3n);

  }
}



PreviousNext

Related