从csv读取文件并删除重复行Java

时间:2015-11-16 18:10:50

标签: java csv

我有带链接的csv文件,它们用逗号分隔。 使用Java读取文件和删除dublicates的最佳方法是什么?

public class CsvChecker {
public void checkDub()  {
    String csvFile = "D:\\links.csv";

}}

1 个答案:

答案 0 :(得分:0)

public class CsvChecker {
public void checkDub() throws IOException {
    String csvFile = "D:\\links.csv";
    BufferedReader br = null;
    String line = "";
    String COMA_DELIMITER = ",";
    String NEW_LINE_SEPARATOR = "\n";
    String csv = "D:\\links2.csv";
    FileWriter fileWriter = new FileWriter(csv);
    HashSet<String> lines = new HashSet<>();



    try {
        br = new BufferedReader(new FileReader(csvFile));
        while ((line = br.readLine()) != null) {
            if (lines.add(line)) {
                fileWriter.append(line);
                fileWriter.append(NEW_LINE_SEPARATOR);
                System.out.println(line);
            }
        }

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (br != null) {
            try {
                br.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}