ADVERTISEMENT
ADVERTISEMENT

Comparing the paths of the two files in Java

Comparing file paths in java: Here, we are going to learn how to compare the paths of two files in java programming language?
Submitted by IncludeHelp, on July 18, 2019

Given the paths of the two files and we have two compare the paths of the files in Java.

Comparing paths of two files

To compare the paths of two files, we can use compareTo() method, it is called with a file object and second file path passes as an argument and method returns 0 if both file paths are the same.

Syntax:

    //file object creation
    File F1 = new File("d://courses//intro.docx");
    File F2 = new File("d://courses//intro.docx");

    //comparing file paths of F1 and F2
    F1.compareTo(F2);

    Output:
    0

Java code to compare the paths of two files

// Java code to compare the paths of 
// two files

import java.io.*;

public class Main {
  public static void main(String[] args) {
    //file object creation
    File F1 = new File("d://courses//intro.docx");
    File F2 = new File("d://courses//intro.docx");
    File F3 = new File("d://courses//basic.docx");

    //comparing file paths using compareTo() method
    //comparing file paths of F1 and F2
    if (F1.compareTo(F2) == 0) {
      System.out.println("paths of F1 and F2 are same...");
    } else {
      System.out.println("paths of F1 and F2 are not same...");
    }

    //comparing file paths of F2 and F3
    if (F2.compareTo(F3) == 0) {
      System.out.println("paths of F2 and F3 are same...");
    } else {
      System.out.println("paths of F2 and F3 are not same...");
    }

    //comparing file paths of F3 and F1
    if (F3.compareTo(F1) == 0) {
      System.out.println("paths of F3 and F1 are same...");
    } else {
      System.out.println("paths of F3 and F1 are not same...");
    }
  }
}

Output

paths of F1 and F2 are same...
paths of F2 and F3 are not same...
paths of F3 and F1 are not same...

Java Files & Directories Programs »


ADVERTISEMENT




ADVERTISEMENT



Comments and Discussions!

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT

ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT

Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.