迭代2个文件以删除任何常见的字符串行并分离出唯一的字符串

时间:2015-09-07 14:21:52

标签: java iterator

public static void main(String[] args) throws IOException {



    FileReader a = new FileReader(new File("C:\\Users\\IBM_ADMIN\\Desktop\\SAM_PUBLIC_MONTHLY_20150802\\a.txt"));  // new file

    FileReader b = new FileReader(new File("C:\\Users\\IBM_ADMIN\\Desktop\\SAM_PUBLIC_MONTHLY_20150802\\b.txt"));  // new file

    @SuppressWarnings("resource")
    BufferedReader file1 =new BufferedReader(a); //new

    @SuppressWarnings("resource")
    BufferedReader file2 =new BufferedReader(b); //old
    PrintWriter  Uniquefile = new PrintWriter (new File("C:\\Users\\IBM_ADMIN\\Desktop\\SAM_PUBLIC_MONTHLY_20150802\\samnew.dat"));

    List<String> list= new ArrayList<String>();
    while(file1.readLine()!=null)
    {
        String str=file1.toString();
        list.add(str);
        while(file2.readLine()!=null)
        {
            String str1=file2.toString();
            if(list.contains(str1))
            {
                list.remove(str);
            }
        }
    }

    Iterator<String> it=list.iterator();
    while(it.hasNext())
    {
        String str2=it.toString();
        Uniquefile.write(str2);
    }
}

我正在迭代2个文件以删除任何常见的字符串行并分离出唯一的字符串。

Ex:文件1 .txt有字符串行&#34; 1 2 3&#34;和文件2.txt有&#34; 2 3&#34; ,我想打印&#34; 1&#34;在文件3.txt中。

希望我的问题很明确。如果有人可以更正下面显示的代码,那将会很棒。感谢

2 个答案:

答案 0 :(得分:2)

如果我理解正确,您希望从两个文件中获取所有不同的行。我建议使用Set收藏

显示它的工作原理是代码片段:

Set<String> set1 = new HashSet<String>();
Set<String> set2 = new HashSet<String>();

String line=null;

while((line=file1.readLine())!=null){
  set1.add(line);
}

while((line=file2.readLine())!=null){
  set2.add(line);
}


//get similars
Set<String> similars=new HashSet<String>(set1);

similars.retainAll(set2);

set1.removeAll(similars);
set2.removeAll(similars);

//all distinct lines
Set<String> distinctSet=new HashSet<String>(set1);
disctinctSet.addAll(set2);

答案 1 :(得分:0)

如果文件不是很大,我的方法是:

  • 阅读这两个文件
  • old
  • 中存储List个值
  • 迭代新值
  • 将非重复值写入新文件

public static void main(String[] args) throws Exception {
    // GET BOTH FILES, BUFFERS AND READERS
    String pathname = "C:\\tmp\\";
    FileReader f1 = new FileReader(new File(pathname, "1.txt"));
    FileReader f2 = new FileReader(new File(pathname, "2.txt"));
    BufferedReader b1 = new BufferedReader(f1);
    BufferedReader b2 = new BufferedReader(f2);

        FileWriter writer = new FileWriter("C:\\Users\\IBM_ADMIN\\Desktop\\SAM_PUBLIC_MONTHLY_20150802\\samnew.dat");

    // STORE OLD VALUES INTO A LIST
    List<String> old = new ArrayList<>();
    String l2 = b2.readLine();
    while (l2 != null) {
        old.add(l2);
        l2 = b2.readLine();
    }

    // ITERATE OVER NEW VALUES
    String line = b1.readLine();
    while (line != null) {
        // IF NOT CONTAINED IN OLD VALUES, PRINT OR WRITE IN FILE
        if (!old.contains(line)) {
            writer.write(line);
            writer.write(System.lineSeparator());

            System.out.println(line);
        }
        line = b1.readLine();
    }

    b1.close();
    b2.close();
}