Q:

Comparing the paths of the two files in Java

belongs to collection: Java Files & Directories Programs

0

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

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

<< Create directory along with required nonexistent p...