1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
/* Compare two file paths This Java example shows how to compare two file paths using compareTo method of Java File class. */ import java.io.*; public class CompareTwoFilePaths { public static void main(String[] args) { //create first file object File file1 = new File("C://FileIO//demo1.txt"); //create second file object File file2 = new File("C://FileIO//demo1.txt"); /* * To compare file paths use, * int compareTo(File file) method of Java File class. * * This method returns 0 of both paths are same, integer less than 0 if * file path is less than that of argument, and positive integer if the * file path is grater than that of argument. */ if(file1.compareTo(file2) == 0) { System.out.println("Both paths are same!"); } else { System.out.println("Paths are not same!"); } } } |
Add Comment