java - 从 csv 文件中删除重复行而不写入新文件

标签 java csv hashset

这是我现在的代码:

File file1 = new File("file1.csv");
File file2 = new File("file2.csv");
HashSet<String> f1 = new HashSet<>(FileUtils.readLines(file1));
HashSet<String> f2 = new HashSet<>(FileUtils.readLines(file2));
f2.removeAll(f1);

使用removeAll(),我从 file1 中删除了 file2 中的所有重复项,但现在我想避免创建新的 csv 文件来优化过程。只是想从 file2 中删除重复的行。

这可能吗还是我必须创建一个新文件?

最佳答案

now I want to avoid to create a new csv file to optimize the process.

嗯,当然,你可以这样做......如果你不介意可能会丢失文件!

不要这样做

既然您使用 Java 7,那么 use java.nio.file 。这是一个例子:

final Path file1 = Paths.get("file1.csv");
final Path file2 = Paths.get("file2.csv");
final Path tmpfile = file2.resolveSibling("file2.csv.new");

final Set<String> file1Lines 
    = new HashSet<>(Files.readAllLines(file1, StandardCharsets.UTF_8));

try (
    final BufferedReader reader = Files.newBufferedReader(file2,
        StandardCharsets.UTF_8);
    final BufferedWriter writer = Files.newBufferedWriter(tmpfile,
        StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW);
) {
    String line;
    while ((line = reader.readLine()) != null)
        if (!file1Lines.contains(line)) {
            writer.write(line);
            writer.newLine();
        }
}

try {
    Files.move(tmpfile, file2, StandardCopyOption.REPLACE_EXISTING,
        StandardCopyOption.ATOMIC_MOVE);
} catch (AtomicMoveNotSupportedException ignored) {
    Files.move(tmpfile, file2, StandardCopyOption.REPLACE_EXISTING);
}

如果您使用 Java 8,则可以使用此 try-with-resources block :

try (
    final Stream<String> stream = Files.lines(file2, StandardCharsets.UTF_8);
    final BufferedWriter writer = Files.newBufferedWriter(tmpfile,
        StandardCharsets.UTF_8, StandardOpenOption.CREATE_NEW);
) {
    stream.filter(line -> !file1Lines.contains(line))
        .forEach(line -> { writer.write(line); writer.newLine(); });
}

关于java - 从 csv 文件中删除重复行而不写入新文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27875560/

相关文章:

java - 从 adb shell 运行基于 android java 的命令行实用程序

java - java中的滑动图像过渡

java - 使用 ffmpeg android 在视频上添加自定义日期

php - Wordpress wpdb - 在自定义插件中插入上传的大型 csv 文件中的记录

python - 用于推断标题行的 `header=True` 的旧 pre-0.17 pandas.read_csv 行为?

java - readLines() 上的 Groovy Reader HashSet 返回乱序

java - 从单独 Web 应用程序的 POJO 类引用 EJB Local home

java - 在java中返回多个map<string,string>

java - 如何在 HashMap/HashSet 中存储两个相等的字符串或任何对象(具有不同的引用)?

csv - 无法将 CSV 复制到 postgreSQL 表中 : timestamp column won't accept the empty string