How to Delete a Specific Line of Text From a File Using Java

preview_player
Показать описание
How to Delete a Specific Line of Text From a File Using Java

Greetings, today we shall be covering how to delete a line or record from a file using Java. In the past we have covered how to delete a line or record from a csv/text file by specifying a search term and deleting the line/record if it matches the search term criteria.

This tutorial differs as we are just going to delete a record/line if it is in a specific line of the file. Eg: If we specify to delete line 1, the first line/record in the file will be deleted. This can be handy in some scenarios if you know which line you wish to delete.

Thanks for watching this deleting a line from a text file using Java tutorial.

Subscribe to keep notified when I upload?:

How to Delete a Specific Line of Text From a File Using Java
Рекомендации по теме
Комментарии
Автор

I'm curious about the theory. Is the temp file method the best? Performance wise, convenience, safety or something?

TheTriangle
Автор

If the file is too big, let say in 100 GB or so and if the last line is the only one eligible to be deleted then you consuming lot of hard disk space with your approach

RajSingh-jgse
Автор

Hi Max thank you for the video, However, I am having issue deleting my original file and replacing it with the temp with .delete( ). I use .deleteOnExit(); in order to delete the file and renameTo. I hope this comments will help some of you out there having the same issue..., but still Max your videos are awesome thank you again.

saiusmc
Автор

how to delete a specific word in a line ?

shortsbyai
Автор

Hey hope you are doing alright just I wanna say that
GOD loved the world so much he sent his only begotten
son Jesus to die a brutal death for us so that we can have eternal life and we can all accept this amazing gift this by simply trusting in Jesus, confessing that GOD raised him from the dead, turning away from your sins and forming a relationship with GOD.

zstar
Автор

Hey Max! Brilliant tutorial! I just have a issue, records.txt file doesn't get deleted and temp.txt file doesn't get removed.

I just found out that:
Result of 'File.delete()' is ignored in oldFile.delete();
Result of 'File.renameTo()' is ignored in newFile.renameTo(dump);

May I ask how can I solve this? I've tried to add a boolean in both but, despite they are not ignored by the IDE, they are not deleted and ranamed :(

New sub btw :)😀

asdasdng
Автор

all my code does is delete the entire contents of the file
public static void removeRecord(String filepath, int deleteline)

{

String tempFile = "temp.txt";
File oldFile = new File(filepath);
File newFile = new File(tempFile);

int line = 0;
String currentLine;

try
{
FileWriter fw = new FileWriter(tempFile, true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);

FileReader fr = new FileReader(filepath);
BufferedReader br = new BufferedReader(fr);

while((currentLine = br.readLine()) !=null);
{
line++;

if (deleteline != line)
{
pw.println(currentLine);
}
}
pw.flush();
pw.close();
fr.close();
br.close();
bw.close();
fw.close();

oldFile.delete();
File dump = new File(filepath);
newFile.renameTo(dump);
}
catch(Exception e){
System.out.println(e);
}
}

retroledeom
join shbcf.ru