如何编辑我的方法比较 [英] how to edit my compare method

查看:123
本文介绍了如何编辑我的方法比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想比较我的两个txt文件组成和写在其他file3.txt文件中的不同的词

I want to compare contens of my two txt files and write the different words in other file3.txt file

我想做这样写另一个txt文件比较的方法。此外,我不
有错误的编码

I want to do compare method in this way to write another txt file. Also I dont have an error for coding

我没有结果。这里是我的code

I don't have a result. here is my code

推荐答案

我已经简化,并纠正了code这个:

I have simplified and corrected your code into this:

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:

I have deneme1.txt:

plane
horoscope
microscope

deneme2.txt:

deneme2.txt:

phone
mobile
plane

在deneme3.txt输出:

Output in deneme3.txt:

horoscope
microscope
mobile
phone

这篇关于如何编辑我的方法比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆