我想实现把两个文本不同处打印至一个新的文本,以下是自己的代码,但是写到最后写不出来了(如何把数组中元素写入到文本?现在半夜3:14了,难道困了?),还有就- package Test01;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.FileReader;
- import java.io.FileWriter;
- import java.io.IOException;
- import java.util.HashSet;
- import java.util.Iterator;
- import com.sun.javafx.geom.AreaOp.AddOp;
- public class Test1_9 {
- public static void main(String[] args)throws IOException
- {
- BufferedReader bfr1 = new BufferedReader(new FileReader("D:\\text1.txt"));
- BufferedReader bfr2 = new BufferedReader(new FileReader("D:\\text2.txt"));
- BufferedWriter bfw2 = new BufferedWriter(new FileWriter("D:\\text4.txt"));
- String len=null;
-
- HashSet<Character> resb = new HashSet<Character>();
- HashSet<Character> resj = new HashSet<Character>();
- HashSet<Character> hs1 = new HashSet<Character>();
- HashSet<Character> hs2 = new HashSet<Character>();
-
- while((len=bfr1.readLine())!=null)
- {
- len.replace(" ", "");
- for (int i = 0; i < len.length(); i++) {
- char ch=len.charAt(i);
- hs1.add(ch);
-
- }
- }
-
- while((len=bfr2.readLine())!=null)
- {
- len.replace(" ", "");
- for (int i = 0; i < len.length(); i++) {
- char ch=len.charAt(i);
- hs2.add(ch);
- }
- }
-
- resb.clear();
- resb.addAll(hs1);
- resb.addAll(hs2);
- resj.clear();
- resj.addAll(hs1);
- resj.retainAll(hs2);
- resb.removeAll(resj);
- System.out.println(resb);
- // Iterator<Character> it=resb.iterator();
- // for(int i=0;i<5&&it.hasNext();i++)
- // {
- // System.out.println(it.next());
- // bfw2.write(it.next());
- // }
- }
- }
复制代码 是感觉代码冗长,大牛们有分享么? |
|