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