【问题标题】:JAVA- copy duplicates from one text file to another- need help removing multiple copies in outputJAVA-将重复项从一个文本文件复制到另一个-需要帮助删除输出中的多个副本
【发布时间】:2022-01-27 09:39:02
【问题描述】:

所以我还不能使用 arrayList 或 hash,因为我们还没有学会它。我已经成功地将副本只复制到了新的文本文件中。现在我不知道如何只将一个副本而不是所有副本复制到新文件中。我想要的输出是: 保护动物 Java 基础 网络编程 单板滑雪 101 output

public static void main(String[] args) throws FileNotFoundException, IOException {
    boolean flag = false;


   String input = ("C:\\Users\\Tyler\\Desktop\\Java Projects\\COMP1231\\"
            + "Assignment 3\\BookTitles.inp");
    String output = "C:\\Users\\Tyler\\Desktop\\Java Projects\\COMP1231\\"
            + "Assignment 3\\DuplicatesBook.inp";
    
    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(output));
        BufferedReader br = new BufferedReader(new FileReader(input));
        
        String s;
        
        while ((s = br.readLine()) != null){
        bw.write(s + "\n");
        flag = false;
        
        BufferedReader br2 = new BufferedReader(new FileReader(output));
        String s2;
        
        while((s2 = br2.readLine()) != null){
            if (s.equals(s2)){
                flag = true;
                break;
            }
            s2 = br2.readLine();
        }
        
        if (!flag){
           bw.write(s + "\n");
            bw.flush();
        }
         s = br.readLine();
    }
        br.close();
        bw.close();
        System.out.println("Files successful.");
    }
    catch (Exception e){
        return;
    }
    
 
        }
    }

【问题讨论】:

  • 数组呢,能用吗?

标签: java duplicates text-files


【解决方案1】:

对于这个问题,我尝试了另一种方法,使用ArrayList<String> 来查找重复的行。代码搜索重复的行并将它们写入输出文件。

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

    String input = ("C:\\Users\\Tyler\\Desktop\\Java Projects\\COMP1231\\"
            + "Assignment 3\\BookTitles.inp");
    String output = "C:\\Users\\Tyler\\Desktop\\Java Projects\\COMP1231\\"
            + "Assignment 3\\DuplicatesBook.inp";
    
    try {
        BufferedWriter bw = new BufferedWriter(new FileWriter(output));
        BufferedReader br = new BufferedReader(new FileReader(input));
        
        ArrayList<String> lines = new ArrayList<>();
        ArrayList<String> duplicates = new ArrayList<>();
        
        String s;
        while ((s = br.readLine()) != null) {
            lines.add(s);
        }
        
        for(String main : lines) {
            if(duplicates.contains(main)) {
                // Skip this line, because we already figured out that it's duplicate
                continue;
            }
            int count = 0;
            for(String sub : lines) {
                if(sub.equals(main)) {
                    // The lines are equal
                    // increase the counter
                    count++;
                }
            } 
            // If the line appears more then once
            // Add it to the duplicate list
            if(count > 1) {
                duplicates.add(main);
            }
        }
        
        // Write all duplicate lines to the BufferedOutputWriter
        for(String duplicate : duplicates) {
            bw.write(duplicate+"\n");
        }
        
        // Don't forget to flush..
        bw.flush();
        
        // Close all inputs and outputs
        bw.close();
        br.close();
        
        System.out.println("All files saved.");
    }
    catch (Exception e) {
        return;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多