how to edit my compare method(如何编辑我的比较方法)
问题描述
我想比较我的两个 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
我没有结果.这是我的代码
I don't have a result. here is my 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 + "
"); }
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
这篇关于如何编辑我的比较方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何编辑我的比较方法


- 未找到/usr/local/lib 中的库 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01
- 转换 ldap 日期 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 获取数字的最后一位 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01