无法比较两个文件。 while 循环不起作用

发布于 2024-12-08 06:56:03 字数 864 浏览 0 评论 0原文

编辑:

我必须文件 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);

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);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

救赎№ 2024-12-15 06:56:03

首先(在提供实际问题的解决方案之前):

index1 = check.indexOf(toCheck);
if(index1 != -1){
    index2++;
    break; //mass1;
}

因为

if(check.equals(checkTo)) { //Or equalsIgnoreCase
    index2++;
    break;
}

Same startSame start, diff end 肯定不一样。

实际问题:
您正在读取 file1 的每一行的 file2 的全部内容。

可能的解决方案

  1. 读取 file1 的内容(逐行),并将其存储在一个 ArrayList File1
  2. 读取 file2 的内容(逐行),并将其存储在另一个 ArrayList File2代码>.
  3. 相互比较这些 ArrayList 的大小,
  4. 如果不相等,则返回(文件肯定不同)。
  5. 否则,循环遍历 ArrayList File1 的每个元素,并使用前面描述的函数与 ArrayList File2 进行比较。

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

  1. Read the contents of file1 (line by line), and store it in an ArrayList File1
  2. Read the contents of file2 (line by line), and store it in another ArrayList File2.
  3. Compare the size of these ArrayLists with each other
  4. If the're not equal, return (the files are definitely different).
  5. Else, loop through each element of ArrayList File1, and compare using the previously described function to ArrayList File2.
爱格式化 2024-12-15 06:56:03

这就是你想要的吗?

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 方法中关闭文件。

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 the containsAll method anymore.
遥远的绿洲 2024-12-15 06:56:03

文件一中的一行与文件二中的每一行都是一样的。那是行不通的。您必须在第二个循环中从文件一获取下一行。

例子:

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
}

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
}
一刻暧昧 2024-12-15 06:56:03

这个方法就能完成工作。

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);
    }

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);
    }
玩物 2024-12-15 06:56:03

看来您不仅要检查 file1 与 file2 中的所有行,还要确定它们不同的行。

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");

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");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文