- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- 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