0

This is my current code:

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);
    }
}

I would like to delete a certain line of text from a file based on user input. For instance, this is my text file:

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

I would like the user to input the String of their choice, this will will be the first field in the column (eg. XX55JKA31), and then have that line of text deleted from the file. I've found some code online, but I've been unable to use it successfully.

My current code seems to just rewrite everything in the temporary text file, but doesn't delete it.

Alex Salinas
  • 25
  • 1
  • 8
  • "My current code seems to just rewrite everything in the temporary text file, but doesn't delete it." So what is this line doing then `tempFile.renameTo(inputFile)` ? – Patrick Parker Feb 28 '17 at 07:55
  • @Patrick Parker It returns false, but I wasn't too sure what I should expect as I had gotten the code elsewhere. The temp file is just recreating my original file without deleting the line that I want. – Alex Salinas Feb 28 '17 at 07:58
  • Possible duplicate of [Reliable File.renameTo() alternative on Windows?](http://stackoverflow.com/questions/1000183/reliable-file-renameto-alternative-on-windows) – Patrick Parker Feb 28 '17 at 08:14

2 Answers2

1

You are using File.renameTo, which is documented here: https://docs.oracle.com/javase/8/docs/api/java/io/File.html#renameTo-java.io.File-

According to the documentation, it may fail if the file already exists, and you should use Files.move instead.

Here is the equivalent code with Files.move:

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

Note: Your code which searches for the VIN is also wrong. See Jure Kolenko's answer for one possible solution to that issue.

Moving forward, you should consider using an actual database to store and manipulate this type of information.

Patrick Parker
  • 4,863
  • 4
  • 19
  • 51
  • Thank you for the help! Your suggestion worked, but I ran into a slight problem that I will have to fix myself. This is just practice, as I am a beginner, but I'll be sure to look into an actual database in the future. – Alex Salinas Feb 28 '17 at 08:41
0

Your error lies in the

if(trimmedLine.equals(lineToRemove)) continue;

It compares the whole line to the VIN you want to remove instead of just the first part. Change that into

if(trimmedLine.startsWith(lineToRemove)) continue;

and it works. If you want to compare to a different column use String::contains instead. Also like Patrick Parker said, using Files.move instead of File::renameTo fixes the renaming problem.

Full fixed code:

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);
    }
}

Note that I changed the class definition not to inherit and the method definition to main(String... args), so I could compile on my system.

Jure Kolenko
  • 799
  • 5
  • 10
  • This works when viewing the temporary file, but the original file wasn't able to update due to an error I'm getting: `Exception in thread "main" java.nio.file.FileSystemException: inventory.txt: The process cannot access the file because it is being used by another process.` – Alex Salinas Feb 28 '17 at 08:26
  • That seems to indicate the file is in use by another process. Try closing any applications which might be using the file or rebooting the system. – Jure Kolenko Feb 28 '17 at 08:29
  • I honestly have no clue. Could it be because the file can be updated in another class? It works if I make another text file, but I guess that means my update class will have to create a duplicate of some kind in order for this Delete_Car class to work. Hmmm... – Alex Salinas Feb 28 '17 at 08:45
  • It's possible the file is also being manipulated in another class. Be careful to close any resources using files. You could use SQLite if you want a simple to use, lightweight database. – Jure Kolenko Feb 28 '17 at 08:49
  • You were correct! It wasn't properly closed in another class that I was using it in. Thanks! I'll definitely be looking into SQL soon. – Alex Salinas Feb 28 '17 at 09:18