Files and Paths
Path Normalize
Normalizing a path removes redundant pieces such as parent-directory markers.
raw path
A raw path can contain navigation pieces such as `..` before it is simplified.
normalized path
A normalized path has those redundant navigation pieces collapsed when possible.
Path Normalize
PathNormalize.java
Replay: real traced execution (multi-file project)
import java.nio.file.Path;
public class PathNormalize {
public static void main(String[] args) {
int draft = 1;
Path raw = Path.of("notes", "drafts", "..", "day" + draft + ".txt");
Path clean = raw.normalize();
System.out.println("raw=" + raw);
System.out.println("clean=" + clean);
}
}
import java.nio.file.Path;
public class PathNormalize {
public static void main(String[] args) {
int draft = 2;
Path raw = Path.of("notes", "drafts", "..", "day" + draft + ".txt");
Path clean = raw.normalize();
System.out.println("raw=" + raw);
System.out.println("clean=" + clean);
}
}
import java.nio.file.Path;
public class PathNormalize {
public static void main(String[] args) {
int draft = 3;
Path raw = Path.of("notes", "drafts", "..", "day" + draft + ".txt");
Path clean = raw.normalize();
System.out.println("raw=" + raw);
System.out.println("clean=" + clean);
}
}
draft ← 1, raw ← notes/drafts/../day1.txt, clean ← notes/day1.txt
3public class PathNormalize {4 public static void main(String[] args) {5 int draft→ 1 = 1; //@draft=2, 36 Path raw→ notes/drafts/../day1.txt = Path.of("notes", "drafts", "..", "day" + draft1 + ".txt");7 Path clean→ notes/day1.txt = raw.normalize();89 System.out.println("raw=" + rawnotes/drafts/../day1.txt);10 System.out.println("clean=" + cleannotes/day1.txt);11 }outputraw=notes/drafts/../day1.txt clean=notes/day1.txt
draft ← 2, raw ← notes/drafts/../day2.txt, clean ← notes/day2.txt
3public class PathNormalize {4 public static void main(String[] args) {5 int draft→ 2 = 2;6 Path raw→ notes/drafts/../day2.txt = Path.of("notes", "drafts", "..", "day" + draft2 + ".txt");7 Path clean→ notes/day2.txt = raw.normalize();89 System.out.println("raw=" + rawnotes/drafts/../day2.txt);10 System.out.println("clean=" + cleannotes/day2.txt);11 }outputraw=notes/drafts/../day2.txt clean=notes/day2.txt
draft ← 3, raw ← notes/drafts/../day3.txt, clean ← notes/day3.txt
3public class PathNormalize {4 public static void main(String[] args) {5 int draft→ 3 = 3;6 Path raw→ notes/drafts/../day3.txt = Path.of("notes", "drafts", "..", "day" + draft3 + ".txt");7 Path clean→ notes/day3.txt = raw.normalize();89 System.out.println("raw=" + rawnotes/drafts/../day3.txt);10 System.out.println("clean=" + cleannotes/day3.txt);11 }outputraw=notes/drafts/../day3.txt clean=notes/day3.txt