Reputation: 21
I Tried this code for deleting certain Lines for my text file. However, when I delete my old file and replace it with new. The file won't be deleted nor be renamed. Can anyone help me with this one ? PS: I tried every method I can find but won't work I need it for my enrollment system project. Thanks in Advance
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");
}
Upvotes: 1
Views: 437
Reputation: 21
Guys Thanks for all your help. I just solved the problem of my code through your ideas. Here is my final code:
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");
}
Upvotes: 0
Reputation: 12450
You're not handling streams correctly. By the time you try to delete / rename the file, it is still open by Java due to the FileReader
/ Scanner
held in print
variable being still open. This will prevent the file from being deleted on Windows.
You need to wrap the reading from file in a try
block and call print.close()
in finally
, or use try-with-resources.
Also, don't forget to close the reader
and writer
the same way (in finally
or using try-with-resources).
Upvotes: 2
Reputation: 989
Use a method of the java.nio. package .. https://docs.oracle.com/javase/tutorial/essential/io/delete.html
It will show you much more information why the file isn't deleted or renamed.
Upvotes: 0