编辑:
我需要文件,FileOne和FileTwo,每行都有一个或多个单词。我想比较这两个文件,看看fileOne的每一行是否与FileTwo的一行完全相同。我用你的想法制作了下面的代码,但是我的结果是小的,这意味着它并没有惹恼fileOne的所有行。下面的代码不会转到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);
答案 0 :(得分:1)
首先(在提供实际问题的解决方案之前):
index1 = check.indexOf(toCheck);
if(index1 != -1){
index2++;
break; //mass1;
}
通过
if(check.equals(checkTo)) { //Or equalsIgnoreCase
index2++;
break;
}
因为行Same start
和Same start, diff end
肯定不一样。
实际问题:
您正在读取file1每行的file2的全部内容。
ArrayList File1
ArrayList File2
中。ArrayList File1
的每个元素,并使用之前描述的函数与ArrayList File2
进行比较。答案 1 :(得分:0)
你是文件一中的一行,文件二中的每一行。这不起作用。你必须在第二个循环中从文件一中获取下一行。
示例:
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
}
答案 2 :(得分:0)
这就是你想要的吗?
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;
}
}
一些评论:
containsAll
看起来与您的要求尽可能相似。loadLines
辅助方法,代码也变得更简单,因为您不再需要担心在containsAll
方法中关闭文件了。答案 3 :(得分:0)
这种方法可以完成这项工作。
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);
}
答案 4 :(得分:0)
看起来你不仅要检查文件2的所有行sin文件1,还要确定它们不同的行。
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");