Home Java Example Java Io File Path compare in java
Questions:Ask|Latest



File Path compare in java
Posted on: February 19, 2011 By Deepak Kumar
Learn how to compare file path in Java?

File Path Comparison :compareTo

    

File Path Comparison :compareTo

In this section, we will discuss how to compare pathname of two file for the equality of their path.  For this purpose, we use   compareTo() method.

First ,we create two instance of class-same or different and check there equality using compareTo() method. The compareTo() method return 0(zero), if the argument is equal to this abstract pathname.

Given below example will give you a clear idea :

Example :

import java.io.File;

public class FilePathCompare {
public static void main(String[] args) {
File file1 = new File("C:/Folder/dev.txt");
File file2 = new File("C:/Folder/dev.txt");
if (file1.compareTo(file2) == 0) {
System.out.println("Both paths are same!");
} else {
System.out.println("Paths are not same!");
}
}
}

Output :

If both path name are same :

Both paths are same!   

Download Source Code


Recommend the tutorial

Ask Questions?    Discuss: File Path compare in java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments