Determining If Two Filename Paths Refer to the Same File - Java File Path IO

Java examples for File Path IO:Path

Description

Determining If Two Filename Paths Refer to the Same File

Demo Code

import java.awt.Graphics;
import java.io.File;
import java.io.IOException;

public class Main {
  public void m() {
    File file1 = new File("./filename");
    File file2 = new File("filename");

    // Filename paths are not equal
    boolean b = file1.equals(file2); // false

    // Normalize the paths
    try {/*from  ww  w.  ja  v a2 s  .  co m*/
      file1 = file1.getCanonicalFile(); // c:\folder\filename
      file2 = file2.getCanonicalFile(); // c:\folder\filename
    } catch (IOException e) {
    }

    // Filename paths are now equal
    b = file1.equals(file2); // true
  }
}

Related Tutorials