使用BufferedReader/BufferedWriter删除文件的第N行 [英] Removing Nth line of File with BufferedReader/BufferedWriter

查看:207
本文介绍了使用BufferedReader/BufferedWriter删除文件的第N行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个这样的文本文件(产品,价格,类别,数量).

So I have a text file structured like this (product,price,category,quantity).

Apple
1.0
Fruits
120
Cucumber
1.4
Vegetables
110
Pear
1.6
Fruits
120

我需要一个可以删除第N个项目的函数,在这种情况下,第二个项目形成了文件(第5、6、7、8行),因此它看起来像这样:

I need a function that can remove the Nth item, in this case the second item form the file (which are lines 5,6,7,8), so it'd look like this:

Apple
1.0
Fruits
120
Pear
1.6
Fruits
120

我能找到的是从文件中删除特定字符串的方法,但是在这种情况下,这不是我所需要的.

All I can find are ways to remove a specific string from file, but that's not what I need in this case.

到目前为止,这是我的代码:

Here's my code so far:

int place=2;            
try{
    File inputFile = new File("Cart.txt"); 
    File tempFile = new File("Cart1.txt"); 
    BufferedReader reader = new BufferedReader(new FileReader(inputFile));
    BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

    int itemToRemove=place*4;
    String currentLine;
    int line=0;
    while((currentLine = reader.readLine()) != null) {
        if(line!=itemToRemove||line!=itemToRemove+1||line!=itemToRemove+2||line!=itemToRemove+3){
            writer.write(currentLine + System.getProperty("line.separator"));
        }
        line++;
    }
    writer.close();
    reader.close();
    tempFile.renameTo(inputFile);
}catch(Exception x){}

它应该从文件中删除第二个项目,但它只是进行复制

It should remove the second item from the file but it just makes a copy

推荐答案

所有您需要做的是使用调试器运行代码,以发现您的 if 条件不正确以及递增的应该是在 while 循环内执行的第一项操作.

All you need to do is run your code with a debugger to discover that your if condition is incorrect and that incrementing line should be the first operation performed inside the while loop.

这是您的验证码.

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public class RemovNth {

    public static void main(String[] args) {
        int place = 2;
        try {
            File inputFile = new File("Cart.txt");
            File tempFile = new File("Cart1.txt");
            BufferedReader reader = new BufferedReader(new FileReader(inputFile));
            BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
            int itemToRemove = (place - 1) * 4;
            String currentLine;
            int line = -1;
            while ((currentLine = reader.readLine()) != null) {
                line++;
                if (line == itemToRemove || line == itemToRemove + 1 || line == itemToRemove + 2
                        || line == itemToRemove + 3) {
                    continue;
                }
                writer.write(currentLine + System.getProperty("line.separator"));
            }
            writer.close();
            reader.close();
            tempFile.renameTo(inputFile);
        }
        catch (Exception x) {
        }
    }
}

请注意,根据文档,方法 renameTo 可能会失败.

Note that, according to the documentation, method renameTo may fail.

您的 if 条件将始终为真.

这篇关于使用BufferedReader/BufferedWriter删除文件的第N行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆