- Java.io package classes
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package extras
- Java.io - Interfaces
- Java.io - Exceptions
- Java.io package Useful Resources
- Java.io - Discussion
Java - File compareTo() Method
Description
The Java File compareTo(File pathname) method compares two abstract pathnames lexicographically. The ordering defined by this method is dependent upon the operating system.
Declaration
Following is the declaration for java.io.File.compareTo(File pathname) method −
public int compareTo(File pathname)
Parameters
pathname − The abstract pathname to be compared to the this abstract pathname.
Return Value
This method returns Zero if the argument is equal to this abstract pathname, negative value and a value greater than 0 if the abstract pathname is lexicographically less than the argument and greater than the argument respectively.
Exception
NA
Example 1
The following example shows the usage of Java File compareTo() method. We've created two File references. Then we're creating these File Objects using test.txt and test1.txt file. Using compareTo() method, we're comparing the paths of the each file lexicographically and printing the result. Lastly we're printing the result of comparison.
package com.tutorialspoint; import java.io.File; public class FileDemo { public static void main(String[] args) { File f = null; File f1 = null; try { // create new files f = new File("File/test.txt"); f1 = new File("File/test1.txt"); // returns integer value int value = f.compareTo(f1); // prints System.out.print("Lexicographically, "); System.out.print("abstract path name test.txt"); // if lexicographically, argument = abstract path name if(value == 0) { System.out.print(" = "); } // if lexicographically, argument < abstract path name else if(value > 0) { System.out.print(" > "); } // if lexicographically, the argument > abstract path name else { System.out.print(" < "); } // print System.out.println("abstract path name File/test1.txt"); // prints the value returned by compareTo() System.out.print("Value returned: "+value); } catch(Exception e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result − assuming that we're having a test.txt and test1.txt file at the given location.
Lexicographically, abstract path name test.txt < abstract path name File/test1.txt Value returned: -3
Example 2
The following example shows the usage of Java File compareTo() method. We've created two File references. Then we're creating these File Objects using test.txt and test1.txt file. Using compareTo() method, we're comparing the paths of the each file lexicographically and printing the result. Here, we've changed the order in which the file was compared in previous example. Lastly we're printing the result of comparison.
package com.tutorialspoint; import java.io.File; public class FileDemo { public static void main(String[] args) { File f = null; File f1 = null; try { // create new files f = new File("File/test.txt"); f1 = new File("File/test1.txt"); // returns integer value int value = f1.compareTo(f); // prints System.out.print("Lexicographically, "); System.out.print("abstract path name test.txt"); // if lexicographically, argument = abstract path name if(value == 0) { System.out.print(" = "); } // if lexicographically, argument < abstract path name else if(value > 0) { System.out.print(" > "); } // if lexicographically, the argument > abstract path name else { System.out.print(" < "); } // print System.out.println("abstract path name File/test1.txt"); // prints the value returned by compareTo() System.out.print("Value returned: "+value); } catch(Exception e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result − assuming that we're having a test.txt and test1.txt file at the given location.
Lexicographically, abstract path name test.txt > abstract path name File/test1.txt Value returned: 3
Example 3
The following example shows the usage of Java File compareTo() method. We've created two File references. Then we're creating these File Objects using test.txt and test2.txt file. Here test2.txt is not present at given location. Using compareTo() method, we're comparing the paths of the each file lexicographically and printing the result. Lastly we're printing the result of comparison.
package com.tutorialspoint; import java.io.File; public class FileDemo { public static void main(String[] args) { File f = null; File f1 = null; try { // create new files f = new File("File/test.txt"); f1 = new File("File/test2.txt"); // returns integer value int value = f.compareTo(f1); // prints System.out.print("Lexicographically, "); System.out.print("abstract path name test.txt"); // if lexicographically, argument = abstract path name if(value == 0) { System.out.print(" = "); } // if lexicographically, argument < abstract path name else if(value > 0) { System.out.print(" > "); } // if lexicographically, the argument > abstract path name else { System.out.print(" < "); } // print System.out.println("abstract path name File/test2.txt"); // prints the value returned by compareTo() System.out.print("Value returned: "+value); } catch(Exception e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result − assuming that we're having a test.txt file at the given location and test2.txt is not present.
Lexicographically, abstract path name test.txt > abstract path name File/test2.txt Value returned: -4