0

我想读取文本文件并显示原始文件和输出文件而没有任何重复的行。

import java.io.*;
import java.util.Scanner;

public class questionOne {


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

        FileInputStream fis = new FileInputStream("text.txt");
        Scanner scanner = new Scanner(fis);


        System.out.println("ORIGINAL FILE: input.txt contains the values   ");


        while(scanner.hasNextLine()){
            System.out.println(scanner.nextLine());
        }

        scanner.close();

        System.out.println("OUTPUT FILE: output.txt contains the values");

        // String a = 

    }   
}
4

1 回答 1

2

您需要使用Set结构来执行此操作。与其显示您已阅读的内容,不如将其存储并打印出来。

使用此方法:

Set<String> uniqueLines = new TreeSet<>();
//in a loop
uniqueLines.add(scanner.nextLine();
// display the set

由于这是一个家庭作业问题,我不会给出完整的答案。祝你的代码好运:)

于 2014-05-09T15:29:01.867 回答