Comparing Path of Two Files in Java Last Updated : 06 Oct, 2022 Comments Improve Suggest changes Like Article Like Report The path of two files can be compared lexicographically in Java using java.io.file.compareTo() method. It is useful to raise a Red Flag by the operating system when the program is requesting the file modification access which is already in use by another program. To compare the path of the file, compareTo() method of File Class is used. compareTo() method compares two abstract path-names lexicographically. The ordering defined by this method is dependent upon the operating system. Parameters: This method requires a single parameter i.e.the abstract pathname that is to be compared. Return Value: This method returns 0 if the argument is equal to this abstract pathname, a negative value if the abstract pathname is lexicographically less than the argument, and a value greater than 0 if the abstract pathname is lexicographically greater than the argument respectively. Example: Java // Comparing path of two files in Java import java.io.File; public class GFG { public static void main(String[] args) { File file1 = new File("/home/mayur/GFG.java"); File file2 = new File("/home/mayur/file.txt"); File file3 = new File("/home/mayur/GFG.java"); // Path comparison if (file1.compareTo(file2) == 0) { System.out.println( "paths of file1 and file2 are same"); } else { System.out.println( "Paths of file1 and file2 are not same"); } // Path comparison if (file1.compareTo(file3) == 0) { System.out.println( "paths of file1 and file3 are same"); } else { System.out.println( "Paths of file1 and file3 are not same"); } } } Output: Comment More infoAdvertise with us M mukulsomukesh Follow Improve Article Tags : Java Java Programs Java-Files Practice Tags : Java Explore BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOPs & InterfacesClasses and Objects in Java10 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java5 min readJava Method References9 min readJava 8 Stream Tutorial15+ min readJava Networking15+ min readJDBC Tutorial12 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions7 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like