Comparing strings from a file

Posted on 2004-11-22
Last Modified: 2010-03-31
Here is a question to roll around in your heads.

if I have 2 files that are identical except a few variables might be different, how do I compare them to see that they match?

the variables look like this:


and there are 2 files that have these types of variables.

How do I read the variable by parameter in one page and find the same parameter in the second file and then compare the values?

Does that make sense?

I appreciate the time.
Question by:k41d3n
    LVL 8

    Accepted Solution

    Hi k41d3n,

    I would use Properties class to read/write the two files.  Properties are stored in (key,value) pairs and it's easy to compare two Properties objects with a loop, something like:

    import java.util.Properties;

    Properties prop1 = new Properties(), prop2 = new Properties();
    FileInputStream in = new FileInputStream(new File("file1"));
    in = new FileInputStream(new File("file2"));
    for (Enumeration e = prop1.propertyNames() ; e.hasMoreElements() ;)
      String key = (String) e.nextElement();
      String value1 = prop1.getProperty(key);
      String value2 = prop2.getProperty(key);
      if(value2 == null)
        System.out.println(key + " is not present in file two");
      else if(value2.compareIgnoreCase(value1) != 0)
        System.out.println(key + " in file 1 has different value than in file two");

    LVL 2

    Author Comment


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
    Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
    Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
    The viewer will learn how to implement Singleton Design Pattern in Java.

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now