Reputation: 11
I have a problem wrtting the code for comparing two files (first reference file):
PROTOCOL STATE SERVICE
1 open icmp
6 open tcp
17 open udp
and (execution file)
PROTOCOL STATE SERVICE
1 open icmp
6 open tcp
17 open udp
255 closed unknown
and save difference between these two files in new file (255 closed unknown).
For comparing I have used following code but it seems it doesn't work.
public String[] compareResultsAndDecide(String refFile, String execFile) throws IOException {
String[] referenceFile = parseFileToStringArray(refFile);
String[] execCommand = parseFileToStringArray(execFile);
List<String> tempList = new ArrayList<String>();
for(int i = 1; i < execCommand.length ; i++)
{
boolean foundString = false; // To be able to track if the string was found in both arrays
for(int j = 1; j < referenceFile.length; j++)
{
if(referenceFile[j].equals(execCommand[i]))
{
foundString = true;
break; // If it exist in both arrays there is no need to look further
}
}
if(!foundString) // If the same is not found in both..
tempList.add(execCommand[i]); // .. add to temporary list
}
String diff[] = tempList.toArray(new String[0]);
if(diff != null) {
return diff;
}
For String refFile
I would use /home/xxx/Ref.txt
path to reference file. And the same for execFile
(second file shown up).
Anyone can help me with this?
Just to add, I'm using for parsing File to String Array:
public String[] parseFileToStringArray(String filename) throws FileNotFoundException {
Scanner sc = new Scanner(new File(filename));
List<String> lines = new ArrayList<String>();
while (sc.hasNextLine()) {
lines.add(sc.nextLine());
}
String[] arr = lines.toArray(new String[0]);
return arr;
}
Upvotes: 1
Views: 1946
Reputation: 3412
Problem is in your .txt
files. Your encoding must be different.
I know this isn't the best way, but if you use replaceAll()
method to replace white spaces from your lines of text files, your code should work. But Unfortunately, you will miss the spaces between lines.
Change:
String[] arr = lines.toArray(new String[0]);
To:
String[] arr = lines.toArray(new String[0]).replaceAll(" ", "");
Note:
trim()
but it didn't worked well for me.0
, not from 1
. Change that too.Upvotes: 0
Reputation: 16498
your compareResultsAndDecide method has to be changed like :
public static String[] compareResultsAndDecide(String refFile, String execFile) throws IOException {
String[] referenceFile = parseFileToStringArray(refFile);
String[] execCommand = parseFileToStringArray(execFile);
List<String> tempList = new ArrayList<String>();
List<String> diff = new ArrayList(Arrays.asList(execCommand));
diff.removeAll(Arrays.asList(referenceFile));
String[] toReturn = new String[diff.size()];
toReturn = diff.toArray(toReturn);
return toReturn;
}
and your parseFileToStringArray like:
public String[] parseFileToStringArray(String filename) throws FileNotFoundException {
Scanner sc = new Scanner(new File(filename));
List<String> lines = new ArrayList<String>();
while (sc.hasNextLine()) {
lines.add(sc.nextLine());
}
String[] arr = new String[lines.size()];
return lines.toArray(arr);
}
Upvotes: 0