File path compare

In this chapter you will learn:

  1. How to compare two file path

Compare two file path

The following methods compare the file paths not the file content.

  • int compareTo(File pathname)
    compares two file lexicographically.
  • boolean equals(Object obj)
    tests this abstract pathname for equality with the given object.
import java.io.File;
//  j a  va2  s. c  om
public class Main {

  public static void main(String[] args) {

    File aFile = new File("c:/aFolder");
    File bFile = new File("c:/bFolder");
    
    System.out.println(aFile.equals(bFile));//false

  }
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to delete a file in Java
  2. How to delete a file on exiting the JVM