删除文本文件中的行

时间:2015-10-12 11:40:10

标签: java

我尝试使用此代码删除文本文件的某些行。但是,当我删除旧文件并将其替换为新文件时。该文件不会被删除也不会被重命名。任何人都可以帮我这个吗? PS:我尝试了我能找到的所有方法,但却没有工作 我需要它用于我的注册系统项目。在此先感谢

public static void deleteStudents() throws IOException, FileNotFoundException
{
    Scanner console = new Scanner(System.in);
    String SY, date;
    System.out.println("ENTER THE SCHOOL YEAR: SY: ");
    SY = console.next();

    int i = 0;

    Scanner print = new Scanner(new FileReader("Students- SY " + SY + " " + ".txt"));
    //display text file
    while(print.hasNextLine())
    {
        String stud= print.nextLine();
        System.out.println(stud);
    }   

    File inputFile = new File("Students- SY " + SY + " " + ".txt");
    File tempFile = new File("Students- SY " + SY + " " + ".txt.bak");

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

    String remove;
    String currentLine;

    System.out.println("ENTER STUDENT's ID NUMBER TO BE DELETED:  ");
    remove = console.next();
    while((currentLine = reader.readLine()) != null)
    {
        String trimmedLine = currentLine.trim();
        if(!trimmedLine.startsWith(remove)) 
        {
            writer.write(String.format("%s%n",currentLine)); 

        }
    }  
    //close reader/writer
    writer.close();
    reader.close();
  //delete file
    if(inputFile.delete())
    {
        tempFile.renameTo(inputFile);
    }
    else
    {
        System.out.println("FAIL");
    }

3 个答案:

答案 0 :(得分:2)

您未正确处理流。当您尝试删除/重命名文件时,由于FileReader变量中的Scanner / print仍处于打开状态,因此它仍然由Java打开。这样可以防止在Windows上删除文件。

您需要在try数据块中包含文件中的阅读内容,并在print.close()中致电finally,或使用try-with-resources

另外,请勿忘记以同样的方式关闭readerwriter(在finally中或使用尝试使用资源

答案 1 :(得分:0)

使用java.nio的方法。包.. https://docs.oracle.com/javase/tutorial/essential/io/delete.html

它会向您显示更多信息,说明文件未被删除或重命名的原因。

答案 2 :(得分:0)

伙计们感谢您的帮助。我刚刚通过你的想法解决了我的代码问题。这是我的最终代码:

public static void deleteStudents() throws IOException, FileNotFoundException
{
    Scanner console = new Scanner(System.in);
    String SY, date;
    System.out.println("ENTER THE SCHOOL YEAR: SY: ");
    SY = console.next();
    //System.out.println("ENTER DATE TO BE DISPLAYED(MM-DD-YY): ");
    //date = console.next();

    File inputFile = new File("Students- SY " + SY + ".txt");
    File tempFile = new File("Students- SY " + SY + ".txt.bak");

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

    Scanner print = new Scanner(new FileReader("Students- SY " + SY + ".txt"));
    while(print.hasNextLine())
    {
        String stud = print.nextLine();
        System.out.println(stud);
    }
    print.close(); 

    String remove;
    String currentLine;

    System.out.println("ENTER STUDENT's ID NUMBER TO BE DELETED:  ");
    remove = console.next();

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

        }
    }  
    writer.close();
    reader.close();

    if(!inputFile.delete())
    {
       tempFile.renameTo(inputFile);
        return;
    }
    if(!tempFile.renameTo(inputFile)){
        System.out.println("Could not rename file");
    }