Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 396621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:34:13+00:00 2026-05-12T16:34:13+00:00

this code is used to compare the content of 2 text files, but i

  • 0

this code is used to compare the content of 2 text files,
but i need to validate first, that the names of those 2 file is not same.

can you help me to fix this code?

 import java.io.*;
 import java.util.*;

 public class compare { 
    public static void main(String args[]) throws IOException {

      try{
        FileInputStream file1 = new FileInputStream(args[0]);
        FileInputStream file2 = new FileInputStream(args[1]);

        if(args.length != 2)
            throw (new RuntimeException("Usage : java compare <filetoread> <filetoread>"));

        if(args[0]==args[1]){
           System.out.print("File name is identical");
        }
         else {
           while (true) {  
             int a = file1.read();
             int b = file2.read();
             if (a!=b) { 
               System.out.print("File do not match");
               break; 
             }
             else{
               System.out.print("Files match");
               break;
             }
           }
         }
      }
      catch(FileNotFoundException e){
        System.out.print("File tidak ada");
      } 
      catch(IOException e){
        System.out.print("IO Error");
      }
   }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T16:34:14+00:00Added an answer on May 12, 2026 at 4:34 pm
     if(args[0].equals(args[1]))
    

    would help to compare the content of the String rather than their addresses.

    Actually

     if(file1.getCanonicalPath().equals(file2.getCanonicalPath())
    

    is better (if you can get File objects ‘new File(arg[0])‘, which can then be used as an argument for your FileInputStream objects): you will compare two canonical normalized path, rather than two String entered by a user with potential lowercase/uppercase differences for instance. See getCanonicalPath() javadoc:

    A canonical pathname is both absolute and unique. The precise definition of canonical form is system-dependent. This method first converts this pathname to absolute form if necessary, as if by invoking the getAbsolutePath() method, and then maps it to its unique form in a system-dependent way. This typically involves removing redundant names such as “.” and “..” from the pathname, resolving symbolic links (on UNIX platforms), and converting drive letters to a standard case (on Microsoft Windows platforms).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code inside a class that is used by an application and
In this thread some one commented that the following code should only be used
In our code we used to have something like this: *(controller->bigstruct) = ( struct
I've seen this format used for comma-delimited lists in some C++ code (although this
I occasionally see the list slice syntax used in Python code like this: newList
When declaring a template, I am used to having this kind of code: template
Maybe this applied to other Delphi's (I've only used 7). We've got our code
I used the wiki in a project hosted at Google Code. With this wiki
I have to compare files with java with a CRC32 code provided by a
I need to compare two files based on datetime. I need to check whether

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.