2017-02-28 101 views
0

这是我当前的代码:使用用户输入从分隔文本文件中删除特定行。 (JAVA)

import java.util.*; 
import java.io.*; 

public class Adding_Deleting_Car extends Admin_Menu { 

     public void delCar() throws IOException{ 
     Scanner in = new Scanner(System.in); 
     File inputFile = new File("inventory.txt"); 
     File tempFile = new File("myTemp.txt"); 

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

     String currentLine; 

     String lineToRemove; 
     System.out.println("Enter the VIN of the car you wish to delete/update: "); 
     lineToRemove = in.next(); 

     while((currentLine = reader.readLine()) != null) { 
      String trimmedLine = currentLine.trim(); 
      if(trimmedLine.equals(lineToRemove)) continue; 
      System.out.println(trimmedLine); 
      writer.write((currentLine) + System.getProperty("line.separator")); 
     } 
     writer.close(); 
     reader.close(); 
     boolean successful = tempFile.renameTo(inputFile); 
     System.out.println(successful); 
    } 
} 

我想从基于用户输入的文件删除文本的某一行。举例来说,这是我的文本文件:

AB234KXAZ;Honda;Accord;1999;10000;3000;G

AB234KL34;Honda;Civic;2009;15000;4000;R

CD555SA72;Toyota;Camry;2010;11000;7000;S

FF2HHKL94;BMW;535i;2011;12000;9000;W

XX55JKA31;Ford;F150;2015;50000;5000;B

我想用户输入自己选择的字符串,这将是f第一栏(例如: XX55JKA31),然后从文件中删除该行文本。我在网上找到了一些代码,但是我一直无法成功使用它。

我目前的代码似乎只是重写临时文本文件中的所有内容,但不会将其删除。

+0

“我目前的代码似乎只是重写临时文本文件中的所有内容,但不会将其删除。”那么这行是什么,然后'tempFile.renameTo(inputFile)'? –

+0

@Patrick Parker它返回false,但我不太确定我应该期待什么,因为我已经在其他地方获得了代码。临时文件只是重新创建我的原始文件,而不删除我想要的行。 –

+0

可能重复的[Reliable File.renameTo()替代Windows?](http://stackoverflow.com/questions/1000183/reliable-file-renameto-alternative-on-windows) –

回答

0

你的错误就在于

if(trimmedLine.equals(lineToRemove)) continue; 

它整体线条比较,要删除,而不是仅仅在第一部分的VIN。改成

if(trimmedLine.startsWith(lineToRemove)) continue; 

它的工作原理。如果您想与不同的色谱柱进行比较,请改用String::contains。也像Patrick Parker说的那样,使用Files.move而不是File :: renameTo来修复重命名问题。

完全固定码:

import java.util.*; 
import java.io.*; 
import java.nio.file.Files; 
import java.nio.file.StandardCopyOption; 



public class Adding_Deleting_Car{ 

     public static void main(String... args) throws IOException{ 
     Scanner in = new Scanner(System.in); 
     File inputFile = new File("inventory.txt"); 
     File tempFile = new File("myTemp.txt"); 

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

     String currentLine; 

     String lineToRemove; 
     System.out.println("Enter the VIN of the car you wish to delete/update: "); 
     lineToRemove = in.next(); 

     while((currentLine = reader.readLine()) != null) { 
      String trimmedLine = currentLine.trim(); 
      if(trimmedLine.startsWith(lineToRemove)) continue; 
      System.out.println(trimmedLine); 
      writer.write((currentLine) + System.getProperty("line.separator")); 
     } 
     writer.close(); 
     reader.close(); 
     Files.move(tempFile.toPath(), inputFile.toPath(), StandardCopyOption.REPLACE_EXISTING); 
    } 
} 

注意,我改变了类定义不继承和方法定义的主要(字符串参数... args),所以我可以编译我的系统上。

+0

这在查看临时文件时有效,但原始文件由于出现错误而无法更新: '线程中的异常“main”java.nio.file.FileSystemException:inventory.txt:The进程无法访问该文件,因为它正在被另一个进程使用。“# –

+0

似乎表示该文件正在被另一进程使用。尝试关闭可能使用该文件的任何应用程序或重新启动系统。 –

+0

我真的没有线索。难道是因为该文件可以在另一个班级更新?它可以工作,如果我做另一个文本文件,但我想这意味着我的更新类将不得不创建一些类型的副本,以便此Delete_Car类工作。嗯...... –

1

您正在使用File.renameTo,这是记录在这里: https://docs.oracle.com/javase/8/docs/api/java/io/File.html#renameTo-java.io.File-

根据该文件,如果文件已经存在,它可能会失败,你应该使用Files.move。

下面是对应的代码Files.move:

boolean successful; 
try { 
    Files.move(tempFile.toPath(), inputFile.toPath(), StandardCopyOption.REPLACE_EXISTING); 
    successful = true; 
} catch(IOException e) { 
    successful = false; 
} 

注: 你的代码搜索的VIN也是错误的。请参阅Jure Kolenko的答案,以解决该问题的一种可能的解决方案。向前迈进,你应该考虑使用一个实际的数据库来存储和操纵这种类型的信息。

+0

谢谢你的帮助!你的建议奏效了,但我遇到了一个小问题,我必须修复自己。这只是练习,因为我是初学者,但我一定会在将来查看实际的数据库。 –

相关问题