Program on How to Compare two file or Directory path - in java file IO



java.io.File's compareTo() method can be used to Compare file or Dictionary path.
Method returns 0 if both paths are same else both paths are not same



Program on How to Compare two file or Directory path - in java file IO >



import java.io.File;
import java.io.IOException;

/** JavaMadeSoEasy.com */
public class ComparePath {
   public static void main(String... args) throws IOException {
          String fileName1 = "c:/myFile.txt";
          String fileName2 = "c:/myFile.txt";
          File file1 = new File(fileName1);
          File file2 = new File(fileName2);
          if (file1.compareTo(file2) == 0)
                 System.out.println(fileName1 + " and " + fileName2 + " are same");
          else
                 System.out.println(fileName1 + " and " + fileName2 + " are not same");
         
   }
}
/* OUTPUT
c:/myFile.txt and c:/myFile.txt are same
*/


RELATED LINKS>

Create File using createNewFile() method in java file IO


Copy a file in java 2 in ways - use org.apache.commons.io.FileUtils's copyDirectory(sourceDirectory, destDirectory)


Program to Move a file - in java file IO


Program to Find file size in bytes, Kilobytes, megabytes and gigabytes using length method of File in java


No comments:

Post a Comment