从文件处理文本文件java中删除特定行

从文件处理文本文件java中删除特定行,java,Java,我有一个问题,我只希望用户只输入球员的名字,例如,如果他输入凯尔·洛瑞,我希望它删除凯尔·洛瑞的所有统计数据。下面是文本字段的示例 File RaptorsFile = new File("raptors.txt"); File tempFile = new File("raptorstemp.txt"); BufferedWriter output = new BufferedWriter (new FileWriter ("raptorstemp.txt"));

我有一个问题,我只希望用户只输入球员的名字,例如,如果他输入凯尔·洛瑞,我希望它删除凯尔·洛瑞的所有统计数据。下面是文本字段的示例

    File RaptorsFile = new File("raptors.txt");
    File tempFile = new File("raptorstemp.txt");
    BufferedWriter output = new BufferedWriter (new FileWriter ("raptorstemp.txt"));
    System.out.println("Please enter the following infomation");
    System.out.println("Please enter the Player's Name"); 
    String PlayerName=sc.nextLine();
    System.out.println("Please enter the Player's FG%"); 
    String FieldGoal=sc.nextLine();
    System.out.println("Please  enter the Player's 3P%"); 
    String ThreePointer=sc.nextLine();
    System.out.println("Please enter the Player's FT%"); 
    String FreeThrow=sc.nextLine();
    System.out.println("Please enter the Player's REB"); 
    String Rebounds=sc.nextLine();
    System.out.println("Please enter the Player's AST"); 
    String Assists=sc.nextLine();
    System.out.println("Please enter the Player's Points"); 
    String Points=sc.nextLine();
    String currentLine;
    while((currentLine = Raptors.readLine()) != null) {
        // trim newline when comparing with lineToRemove
        String trimmedLine = currentLine.trim();
        if(trimmedLine.equals(PlayerName+"," +FieldGoal+"," +ThreePointer+"," +FreeThrow+"," +Rebounds+"," +Assists+"," +Points)) continue;
        output.write(currentLine + System.getProperty("line.separator"));
    }

    boolean successful = tempFile.renameTo(RaptorsFile);
    output.close();
    System.out.println("The CarsTemp file will have the new list"); 
    options(Raptors,s);

可以使用String.startsWith()方法,也可以与正则表达式匹配

解决方案1 此解决方案具有更好的性能,但可能导致误报

解决方案2 此解决方案完全符合您的标准,但性能更差,看起来更难看

我认为解决方案1应该很有效。 请注意,我没有测试上述代码

PS:遵守官方的编码惯例总是一个好主意,比如变量名应该以小写字母开头等等


PS2:您可以使用PrintWriter而不是BufferedWriter,后者为您提供了额外的方法,例如println,它结合了
write(…)
newLine()

,因此基本上您可以循环文件并检查每行文本的开头,以查看它是否从用户输入开始。可以使用String方法startsWith()确定行是否以用户输入开始,如果返回false,则将行写入临时文件,如果返回true,则跳到输入文件中的下一行。扫描完整个输入文件后,请同时关闭它们,将原始文件重命名为备份文件,并将临时文件重命名为原始文件。这不是一个非常复杂的问题。

基本上如果用户输入Kyle Lowry,我希望它删除整行,基本上删除Kyle Lowry,44.9,35.1,81.3160260702,并保留restIs
sc
a
Scanner
?@Ascalonian是的,Scanner sc=new Scanner(System.in);您将为此使用临时文件。这里有一个例子:或者@Ascalonian,我不明白,这让我很困惑,我需要这个文件tempFile=new文件(“raptorstemp.txt”);?因为我不想让人困惑,因为这是我的最后一个项目,我也不想使用System.getPropert,因为idk如何使用它,我问我的同学你有两个选择:你可以将整个文件内容读入内存,然后你不需要临时文件。或者您可以保持现在的状态,这样您就可以并行地从一个文件读取和写入另一个文件,最后删除旧文件并重命名临时文件。我更新了我的答案并删除了System.getProperty(…)。我想做选项一,你能帮我一下吗?我被困在这里很长时间了。我能做这个吗?或者选项1比我的容易吗?因为我不想和while一起工作((currentLine=Raptors.readLine())!=null){//trim newline当与lines比较时删除String trimmeline=currentLine.trim();if(trimmeline.equals)(PlayerName+,“+FieldGoal+”,“+ThreePointer+,“+罚球+”,“+篮板+,“+助攻+”,”+Points)continue;output.write(currentLine+System.getProperty(“line.separator”);}boolean successful=tempFile.renameTo(RaptorsFile);output.close();System.out.println(“CarsTemp文件将具有新列表”);大卫的回答很好地涵盖了这一点,我不确定我是否理解是什么造成了混乱。
Lou Williams,41.1,36.3,87.6,60,53,508
Kyle Lowry,44.9,35.1,81.3,160,260,702
Patrick Patterson,48.8,46.0,75.0,177,61,286
Terrence Ross,42.9,38.7,87.9,119,30,411
Jonas Valanciunas,54.2,0.0,79.2,283,16,414
 while ((currentLine = Raptors.readLine()) != null) {
    if (!currentLine.startsWith(PlayerName + ",")) { // you could use continue as well, but imho it is more readable to put the output.write inside the condition
        output.write(currentLine);
        output.newLine();
    }
 }
 while ((currentLine = Raptors.readLine()) != null) {
    if (!currentLine.matches(PlayerName + ",\\d[\\d.+],\\d[\\d.+],\\d[\\d.+]),\\d+\\d+,\\d+\\s*") { 
        output.write(currentLine);
        output.newLine();
    }
 }