java 使用java删除文本文件中的一行

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11645881/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:54:15  来源:igfitidea点击:

deleting a row of line in a text file using java

javafilefile-iodelete-row

提问by simple pogi87x

I'm having a hard time deleting a row of lines in a text file, I've use this code but i end up deleting all the lines instead, need some help.

我很难删除文本文件中的一行行,我使用了这段代码,但我最终删除了所有行,需要一些帮助。

    try //vacation leave/
{
    File inputFile = new File("Adlawan" + code1);
    File tempFile = new File("AdalwanTempFile");

    BufferedReader reader = new BufferedReader(new FileReader(inputFile));
    BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

    String lineToRemove = "AdlawanJan2012";
    String currentLine;

    while((currentLine = reader.readLine()) != null)
    {
        //trim newline when comparing with lineToRemove
        String trimmedLine = currentLine.trim();
        if(trimmedLine.equals(lineToRemove)) 
        {
         writer.write(currentLine); 
        }
    }   
    writer.close();
    reader.close();

    if(!inputFile.delete())
    {
        JOptionPane.showMessageDialog(null, "Could not rename file");
        return;
    }
    if(!tempFile.renameTo(inputFile))
        JOptionPane.showMessageDialog(null, "Could not rename file");


}
catch(Exception e)
{
    JOptionPane.showMessageDialog(null, " ");
}

The data on the file looks like this:

文件上的数据如下所示:

AdlawanJan2012 Vacation-Leave-Credits -0.6875
AdlawanFeb2012 Vacation-Leave-Credits -0.6875
AdlawanMar2012 Vacation-Leave-Credits -0.6875

AdlawanJan2012 Vacation-Leave-Credits -0.6875
AdlawanFeb2012 Vacation-Leave-Credits -0.6875
AdlawanMar2012 Vacation-Leave-Credits -0.6875

Desired result after trimming the lines:

修剪线条后所需的结果:

AdlawanFeb2012 Vacation-Leave-Credits -0.6875
AdlawanMar2012 Vacation-Leave-Credits -0.6875

AdlawanFeb2012 Vacation-Leave-Credits -0.6875
AdlawanMar2012 Vacation-Leave-Credits -0.6875

Thank you...

谢谢...

回答by hidemaru

I think you're trying to delete line in a file if the line starts with the "lineToRemove"variable, in that case you might want to use "startsWith"method instead of "equal"method.

如果行以“lineToRemove”变量开头,我认为您正在尝试删除文件中的行,在这种情况下,您可能想要使用“startsWith”方法而不是“equal”方法。

while((currentLine = reader.readLine()) != null)
{
    //trim newline when comparing with lineToRemove
    String trimmedLine = currentLine.trim();
    if(!trimmedLine.startsWith(lineToRemove)) 
    {
        // if current line not start with lineToRemove then write to file
        writer.write(currentLine); 
    }
}

回答by jayantS

trim()method only removes leading and trailing white spaces and not characters after white space.

trim()方法只删除前导和尾随空格,而不是空格后的字符。

String lineToRemove = "AdlawanJan2012";


....


....

String trimmedLine = currentLine.trim();
if(trimmedLine.substring(0, 14).equals(lineToRemove)) {
    //Your deletion logic
}

回答by Olu Smith

It'a a good idea to read the file content line by line and copy them to another file(temporal file) preferably a Random Access file and omit the line you want to remove. just copy the rest to the temporal file and omit the line(s) you want to delete. then rename the file to original name. No samples Please. I need you to do this on your own that way you'll learn all by your self. good luck

逐行读取文件内容并将它们复制到另一个文件(临时文件)最好是随机访问文件并省略要删除的行是一个好主意。只需将其余部分复制到临时文件并省略要删除的行。然后将文件重命名为原始名称。没有样品请。我需要你自己做这件事,这样你就可以自学了。祝你好运