EDIT:
i have to files, FileOne and FileTwo, which in every line there is a word开发者_开发百科 or more. and i want to compare these two file, and see if every line of fileOne is exact the same or piece of a line of FileTwo. I made the code below with your ideas, but i my result is to small that means that it is not ckecking all the lines of fileOne. The code below, isn't going to the next object of it1?
int index1 = 0;
int index2 = 0;
ArrayList <String> File1 = File2List(FileOne);
Iterator it1 = File1.listIterator();
ArrayList <String> File2 = File2List(FileTwo);
Iterator it2 = File2.listIterator();
while (it1.hasNext()) {
String outer = it1.next().toString();
while (it2.hasNext()) {
String inner = it2.next().toString();
index1 = outer.indexOf(inner);
if(index1 != -1) { //Or compareIgnoreCase
index2++;
it1.next();
break;
}
}
it1.next();
}
System.out.println("Result: "+ index2);
First (before offering the solution to the actual issue):
index1 = check.indexOf(toCheck);
if(index1 != -1){
index2++;
break; //mass1;
}
by
if(check.equals(checkTo)) { //Or equalsIgnoreCase
index2++;
break;
}
Because the line Same start
and Same start, diff end
are certainly not the same.
Actual issue:
You're reading the whole contents of file2 at each line of file1.
Possible solution
- Read the contents of file1 (line by line), and store it in an
ArrayList File1
- Read the contents of file2 (line by line), and store it in another
ArrayList File2
. - Compare the size of these ArrayLists with each other
- If the're not equal, return (the files are definitely different).
- Else, loop through each element of
ArrayList File1
, and compare using the previously described function toArrayList File2
.
Is that what you want?
package so7625322;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class FindWords {
private static List<String> loadLines(String fname) throws IOException {
InputStream is = new FileInputStream(fname);
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, "UTF-8"));
List<String> lines = new ArrayList<String>();
String line;
while ((line = rd.readLine()) != null) {
lines.add(line);
}
return lines;
} finally {
is.close();
}
}
private boolean contains(Iterable<String> haystack, String needle) {
for (String s : haystack) {
if (s.contains(needle)) {
return true;
}
}
return false;
}
public boolean containsAll() throws IOException {
List<String> words = loadLines("./NetBeansProjects/StemmerListComp/StemmedUnTunedQueries.txt");
List<String> tocheck = loadLines("words-to-check");
for (String s : tocheck) {
if (!contains(words, s)) {
return false;
}
}
return true;
}
}
Some remarks:
- I have split the task into several small tasks, so that the main method
containsAll
looks as similar to your requirement as possible. - Using the
loadLines
helper method the code also becomes simpler, since you don't need to be concerned about closing the files in thecontainsAll
method anymore.
You are one line from file one with every line from file two. That does not work. You have to get the next lines from file one in the second loop.
Example:
while (it1.hasNext()) {
String outer = it1.next().toString();
if(!it2.hasNext()) {
// FALSE!
} else {
String inner = it2.next().toString();
if(!inner.compare(outer)) { //Or compareIgnoreCase
// FALSE
}
}
}
if(it2.hasNext()) {
// FALSE
}
This method will do the work.
private bool IsIdentical(string filePath1, string filePath2)
{
int file1Byte, file2Byte;
FileStream fileStream1, fileStream2;
//Open a stream to both files
fileStream1 = new FileStream(filePath1, FileMode.Open);
fileStream2 = new FileStream(filePath2, FileMode.Open);
// Check the file sizes to determine if the files are identical.
if (fileStream1.Length != fileStream2.Length)
{
fileStream1.Close();
fileStream2.Close();
return false;
}
//Read one byte from each file and compare them until either a non-matching
//set of bytes is found or until the end of the file.
do
{
// Read one byte from each file.
file1Byte = fileStream1.ReadByte();
file2Byte = fileStream2.ReadByte();
}
while ((file1Byte == file2Byte) && (file1Byte != -1));
fileStream1.Close();
fileStream2.Close();
return ((file1Byte - file2Byte) == 0);
}
It appears that you not only want to check is all the line sin file1 against file2 but also determine the line where they differ.
List<String> file1 = File2List(fileOne);
List<String> file2 = File2List(fileTwo);
for(int i=0;i<file1.size() && i<file2.size();i++)
if(!file2.get(i).contains(file1.get(i))) {
System.out.println("mismatch at line "+i);
return;
}
if (file1.size()<file2.size()) {
System.out.println("file1 is shorter than file2");
return;
}
if (file2.size()<file1.size()) {
System.out.println("file2 is shorter than file1");
return;
}
System.out.println("no mismatches found");
精彩评论