0

我想比较我的两个 txt 文件的内容并在其他 file3.txt 文件中写入不同的单词

我想用这种方式做比较方法来写另一个txt文件。我也没有编码错误

我没有结果。这是我的代码

4

5 回答 5

3

我已将您的代码简化并更正为:

public class TextAreaSample
{
  public static void main(String [] args) throws IOException {
    compare(readFileAsList("deneme1.txt"),
            readFileAsList("deneme2.txt"));
  }

  private static void compare(List<String> strings1, List<String> strings2)
  throws IOException
  {
    final Collator c = Collator.getInstance();
    c.setStrength(Collator.PRIMARY);
    final SortedSet<String>
      union = new TreeSet<String>(c),
      intersection = new TreeSet<String>(c);
    union.addAll(strings1);
    union.addAll(strings2);
    intersection.addAll(strings1);
    intersection.retainAll(strings2);
    union.removeAll(intersection);
    write(union, "deneme3.txt");
  }

  private static void write(Collection<String> out, String fname) throws IOException {
    FileWriter writer = new FileWriter(new File(fname));
    try { for (String s : out) writer.write(s + "\n"); }
    finally { writer.close(); }
  }

  private static List<String> readFileAsList(String name) throws IOException {
    final List<String> ret = new ArrayList<String>();
    final BufferedReader br = new BufferedReader(new FileReader(name));
    try {
      String strLine;
      while ((strLine = br.readLine()) != null) ret.add(strLine);
      return ret;
    } finally { br.close(); }
  }
}

我有 deneme1.txt:

plane
horoscope
microscope

deneme2.txt:

phone
mobile
plane

deneme3.txt 中的输出:

horoscope
microscope
mobile
phone
于 2012-08-03T09:30:07.253 回答
3

我刚刚使用以下文件运行了您的程序,但无法重现您的问题。

定名1

abc
def
ghi

定位基因2

abc
ghi
klm

并且deneme3是使用以下内容创建的:

abc
ghi

编辑

看来你想要相反的行为。您的某些方法不必要地复杂,并且可以通过使用标准 JDK 的正确工具来缩短。请参阅下面的简化实现示例(仅保留两个文件之间不通用的单词)-此示例区分大小写

public class TextAreaSample {

    public static void main(String[] args) throws IOException {
        //readAllLines does what you do in readFileAsList
        List<String> strings1 = Files.readAllLines(Paths.get("C:/temp/deneme1.txt"), Charset.defaultCharset());
        List<String> strings2 = Files.readAllLines(Paths.get("C:\\temp\\deneme2.txt"), Charset.defaultCharset());
        Set<String> notInCommon = getNotInCommon(strings1, strings2);
        write(notInCommon, "C:\\temp\\deneme3.txt");
    }

    private static void write(Collection<String> out, String fname) throws IOException {
        FileWriter writer = new FileWriter(new File("C:\\temp\\deneme3.txt"));
        for (String s : out) {
            writer.write(s + "\n");
        }
        writer.close();
    }

    private static Set<String> getNotInCommon(List<String> strings1, List<String> strings2) {
        //Sets are great to get unique lists and check commonality
        Set<String> onlyInFile1 = new HashSet<String>(strings1);
        onlyInFile1.removeAll(strings2); //remove strings in s1 AND s2
        Set<String> onlyInFile2 = new HashSet<String>(strings2);
        onlyInFile2.removeAll(strings1); //remove strings in s1 AND s2
        Set<String> notInCommon = new HashSet<>();
        notInCommon.addAll(onlyInFile1);
        notInCommon.addAll(onlyInFile2);

        return notInCommon;
    }
}
于 2012-08-03T09:25:27.283 回答
0

我的建议是不要试图一次性解决所有问题。您可以通过使用一个班轮来简化您的比较方法 strings1.retainAll(strings2)

有关更多信息,请参阅此 http://docs.oracle.com/javase/6/docs/api/java/util/Collection.html#retainAll(java.util.Collection )

并打印 strings1 的内容,看看是否可以,然后解决文件写入部分。

于 2012-08-03T09:25:39.407 回答
0

您将第三个文件打开deneme3.txt两次,但中间没有关闭它。我猜第二次(in write())会抛出异常,所以不会写。删除FileWriter writer = new FileWriter(new File("D:\\Denemeler\\deneme3.txt"));(中的那个compare())的第一次出现,你应该没问题。

于 2012-08-03T09:29:17.677 回答
-2

我认为您必须在关闭它之前刷新()您的作家。

private static void write(ArrayList<String> out, String fname) throws IOException {
    FileWriter writer = new FileWriter(new File("D:\\Denemeler\\deneme3.txt"));
    for (int i = 0; i < out.size(); i++) {
        writer.write(out.get(i) + "\n");
    }
    // Flush the writer before closing it.
    writer.flush();

    writer.close();
}
于 2012-08-03T09:16:40.727 回答