 
  Data Structure Data Structure
 Networking Networking
 RDBMS RDBMS
 Operating System Operating System
 Java Java
 MS Excel MS Excel
 iOS iOS
 HTML HTML
 CSS CSS
 Android Android
 Python Python
 C Programming C Programming
 C++ C++
 C# C#
 MongoDB MongoDB
 MySQL MySQL
 Javascript Javascript
 PHP PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Compare two file paths in Java
Two file paths can be compared lexicographically in Java using the method java.io.File.compareTo(). This method requires a single parameter i.e.the abstract path name that is to be compared. It returns 0 if the two file path names are equal.
A program that demonstrates this is given as follows −
Example
import java.io.File; public class Demo {    public static void main(String[] args) {       File file1 = new File("C:/File/demo1.txt");       File file2 = new File("C:/File/demo1.txt");       if (file1.compareTo(file2) == 0) {          System.out.println("Both the paths are lexicographically equal");       } else {          System.out.println("Both the paths are lexicographically not equal");       }    } } The output of the above program is as follows −
Output
Both the paths are lexicographically equal
Now let us understand the above program.
The method java.io.File.compareTo() is used to compare the two file paths lexicographically. If 0 is returned by the method, the file paths are lexicographically equal, otherwise not. A code snippet that demonstrates this is given as follows −
File file1 = new File("C:/File/demo1.txt"); File file2 = new File("C:/File/demo1.txt"); if (file1.compareTo(file2) == 0) {    System.out.println("Both the paths are lexicographically equal"); } else {    System.out.println("Both the paths are lexicographically not equal"); }Advertisements
 