Java I/O How to - Convert a filename path to a unique canonical form suitable for comparisons








Question

We would like to know how to convert a filename path to a unique canonical form suitable for comparisons.

Answer

 //from   w w  w . jav  a2  s .  co m

import java.io.File;

public class Main {
  public static void main(String[] argv) throws Exception {

    File file1 = new File("./filename");
    File file2 = new File("filename");
    System.out.println(file1.equals(file2));

    file1 = file1.getCanonicalFile();
    file2 = file2.getCanonicalFile();
    System.out.println(file1.equals(file2));
  }
}

The code above generates the following result.