如何编辑我的比较方法

时间:2012-08-03 08:58:36

标签: java arrays string compare filewriter

我想比较我的两个txt文件的竞争并在其他file3.txt文件中写下不同的单词

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

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

5 个答案:

答案 0 :(得分:3)

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

<强> deneme1

abc
def
ghi

<强> deneme2

abc
ghi
klm

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

abc
ghi

修改

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

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

答案 1 :(得分: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

答案 2 :(得分:0)

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

有关详情,请参阅此处 http://docs.oracle.com/javase/6/docs/api/java/util/Collection.html#retainAll(java.util.Collection

然后打印strings1的内容并查看它是否正常,然后解决该文件写入部分。

答案 3 :(得分:0)

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

答案 4 :(得分:-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();
}