Following example shows how to compare paths of two file using file1.compareTo() method in Java.
compareTo
public int compareTo(File pathname)
Compares two abstract pathnames lexicographically. The ordering defined by this method depends upon the underlying system. On UNIX systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows systems it is not.
Sample Program
CompareFilePath.java
Result
The above code will produce the following output.
compareTo
public int compareTo(File pathname)
Compares two abstract pathnames lexicographically. The ordering defined by this method depends upon the underlying system. On UNIX systems, alphabetic case is significant in comparing pathnames; on Microsoft Windows systems it is not.
Sample Program
CompareFilePath.java
package com.javatutorialcorner.file; import java.io.File; public class CompareFilePath { public static void main(String[] args) { File jtcFile1 = new File("/home/annamalai/workspace/File1/jtc.txt"); File jtcFile2 = new File("/home/annamalai/workspace/File2/jtc.txt"); if(jtcFile1.compareTo(jtcFile2) == 0) { System.out.println("Both paths are same!"); } else { System.out.println("Paths are not same!"); } } }
Result
The above code will produce the following output.
Paths are not same!
0 comments:
Post a Comment