Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Comparing strings from a file

Posted on 2004-11-22
2
Medium Priority
?
271 Views
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:

parameter=value

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.
0
Comment
Question by:k41d3n
2 Comments
 
LVL 8

Accepted Solution

by:
inq123 earned 2000 total points
ID: 12647638
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"));
prop1.load(in);
in.close();
in = new FileInputStream(new File("file2"));
prop2.load(in);
in.close();
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");
}

Cheers!
0
 
LVL 2

Author Comment

by:k41d3n
ID: 12648016
Glorious.
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

577 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