Monday 14, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Tuesday, 12 May 2015

How to compare paths of two file using Java

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
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!

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to convert a String to Lower caseFollowing example shows how to convert a string to lower case. Sample Programpackage com.javatutorialcorner.javastring;public class ToLowerCase { public static void main(String[] args) { String input = "Java tutoria… Read More
  • How to change File Last modified Date using Java Following example shows how to change file last modified date using file.setLastModified() method in Java. setLastModifiedpublic boolean setLastModified(long time)  Sets the last-modified time of the file or directory … Read More
  • How to add value to all types of Collections Following example shows how to add values in all types collections in Java. Sample Program AddValueToCollection.java package com.javatutorialcorner.collection; import java.util.ArrayList; import java.util.Collection; impo… Read More
  • How to reverse the Collection (List) Following example shows how to reverse the collection (List) using Collections.reverse(arg0); method in Java. void java.util.Collections.reverse(List<?> list)reversepublic static void reverse(List<?> list) Rever… Read More
  • How to Split the String in JavaFollowing Example shows how to split the string using str.split(String pattern) method in Java. Sample Programpackage com.javatutorialcorner.javastring;public class SplitString { public static void main(String[] args) { … Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to compare paths of two file using Java Rating: 5 Reviewed By: eHowToNow