Compare two file paths in Java

Description

The following code shows how to compare two file paths.

Example


//  w ww  .jav  a2s. c om
import java.io.File;

public class Main {

  public static void main(String[] args) {
    File file1 = new File("C:/File/demo1.txt");
    File file2 = new File("C:/FileIO/demo1.txt");

    if (file1.compareTo(file2) == 0) {
      System.out.println("Both paths are same!");
    } else {
      System.out.println("Paths are not same!");
    }
  }
}

The code above generates the following result.





















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip