Java 从csv文件中删除重复行,而不写入新文件

Java 从csv文件中删除重复行,而不写入新文件,java,csv,hashset,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

这是我目前的代码:

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);
File file1=新文件(“file1.csv”);
文件file2=新文件(“file2.csv”);
HashSet f1=新的HashSet(FileUtils.readLines(file1));
HashSet f2=新的HashSet(FileUtils.readLines(file2));
f2.移除所有(f1);
使用
removeAll()
我从文件1中删除文件2中的所有重复项,但现在我希望避免创建新的csv文件来优化流程。只想从文件2中删除重复的行


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

我用这行代码解决了这个问题:

FileUtils.writeLines(file2, f2);
它是一种覆盖,可以很好地解决中小型文件, 但对于非常大的数据集,我真的不知道

现在我想避免创建新的csv文件来优化流程

当然,你可以做到。。。如果你不介意可能会丢失文件

不要那样做

既然您使用的是Java7,那么。下面是一个例子:

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);
}
final Path file1=Path.get(“file1.csv”);
最终路径file2=Path.get(“file2.csv”);
最终路径tmpfile=file2.resolveSibling(“file2.csv.new”);
最终设置文件1行
=新哈希集(Files.readAllLines(file1,StandardCharsets.UTF_8));
试一试(
final BufferedReader reader=Files.newBufferedReader(file2,
标准字符集(UTF_8);
final BufferedWriter writer=Files.newBufferedWriter(tmpfile,
StandardCharsets.UTF_8,StandardOpenOption.CREATE_NEW);
) {
弦线;
而((line=reader.readLine())!=null)
如果(!file1Lines.contains(行)){
作者:写(行);
writer.newLine();
}
}
试一试{
文件.move(tmpfile、file2、StandardCopyOption.REPLACE_-EXISTING、,
标准复制选项。原子移动);
}捕获(忽略AtomicMoveNotSupportedException){
移动(tmpfile、file2、StandardCopyOption.REPLACE_EXISTING);
}
如果您使用Java 8,则可以改用这个try with resources块:

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(); });
}
试试看(
最终流=Files.lines(file2,StandardCharsets.UTF_8);
final BufferedWriter writer=Files.newBufferedWriter(tmpfile,
StandardCharsets.UTF_8,StandardOpenOption.CREATE_NEW);
) {
stream.filter(行->!file1Lines.contains(行))
.forEach(行->{writer.write(行);writer.newLine();});
}

是否有机会为上述任务实现数据库的备份?您是否只需将f2结果存储回文件?并不是您不想将整个文件写入磁盘(我认为,随机访问文件在这里是没有选项的),而是您有两个文件,但只想要/需要一个,对吗?是的,我只需要一个文件,因为它是最新的。因此,这一个不需要存储在数据库中的旧记录,而只需要新记录。这个函数返回一个布尔值,所以我不知道如何分配回file2。啊,好的,我想现在我明白了。创建文件1并保存一些数据,然后对这些数据进行处理。稍后创建的文件2还包含文件1中的数据。在处理文件2时,您不希望再次处理已在上的文件中处理的数据。因此,在你的代码之后,你就有了f2中实际需要的数据。因此,最后一步就是将f2的内容写回file2。您可以使用removeAll的返回来查看是否有必要写入新文件<如果此集合因调用而更改,则code>返回true