Just a little note, at 1:19 the record containing '2331,yoyo,56' isn't meant to be written to temp.txt until after we have found the record we wish to delete (1:33), so I am sorry for any confusion my little editing error made.
I've literally copied the whole code of yours and the IO exception returns null. Dunno why, the only difference is that I have more String variables and use semicolons instead of commas as separators between the fields. (Yes I modified your code respectively). If you could help me, I would be grateful.
The approach was quite workable. Seemingly this approach allows to get away with minimal number of imported Java libraries and without importing "java.nio" library (Java's new input/output library). I worked out this approach on Java 8_update144. Code worked.
First of all great explanation understood what you were explaining, but im having an issue i'm the delete file line keeps returning false i have ensured that all printwriters are closed and also check the text file permissions but still having difficulty deleting the file. Do you have any idea why this is happening?
Hi Max, why doesn't this code work when you call it from another class?It writes to temp file fine, but it doesnt rename temp file to the old file name.Thanks !
well i have a printwriter and i want after it executes once the next time it executes to change a few lines of of it or to simply delete the whole file so it can be re written when i execute. is this even possible?
I need help on a project that deals with a client/server where I will need to read/add/remove/edit a csv file. These videos have been good you think you can help me out on this with a video.
[Please read it, although it's quite damn long!] Hi! I have a problem. Every very first time I substitute the file for temp.txt (either deleting or editing the file), NetBeans becomes literally unable to read anything which concerns the last item in the record (in this case age) in the other methods that read from edited file. Even though, I put e.g. System.print.out.ln("age:" + age) between x.next lines and if condition in the while (x hasNext()) loop, it just prints the blank line, whereas all other variables, such as ID and name are printed! What is interesting, the NetBeans is able to write the last item (age) to the arrayList, though, when I am running my program. Nevertheless, I am unable to make any comparisons (editions and deletiins as well) of anything that concerns the file with once made temp-procedure, as any comparisons with blank value of the last item of each record cannot be made. Max (or anybody else) please help! Thanks in advance!
I have a problem, where I have set the deleteRecord as a class and I would like to call it in a main function. However I have encountered a problem that the when I wanted to call the removeRecord() with the argument of filepath and removeTerm , it is not working.
@@maxodidily Firstly, I would like to apologize for the super duper late response :/ but I managed to fixed it due to my stupid mistakes. It's just that I called two of the methods in one class, where the first method has already called the second method. So my problem has been resolved. Also a huge thanks to you for providing this tutorial! I really really do appreciate it and keep doing what you like Max! :D
This program is for delete line when data is found on the first split, but didn't do anything if data found after the second and last split. Please teach us to do is data found in last split. Your help is very appreciated.
You can easily modify this code to check any field of a record. 'x.next' reads a field of a record, I use 'x.next' multiple times to read all fields of a record. In this tutorial we check the first field of a record (the first 'x.next') but you can easily just compare any 'x.next' to the deleteTerm variable.
Sir, how to make it work with a .csv extension? It's working with txt but not working with csv extension. Please revert asap its required for a project
@@shellywu4945 Thanks for clarifying, you would have to pass in multiple deleteTerms into the fucntion, either as multiple strings or as a string array. I would advise a String array, call it String[] deleteTermsArray and instead of 'If(!ID.equals(deleteTerm))' you could do 'If(!Arrays.asList(deleteTermsArray).contains(ID))'. Hope this helps! You could also use an arraylist instead of a String array, I have a tutorial series on arraylists here: ruclips.net/video/TCsNrGKC340/видео.html
It amazes me how nothing works smoothly with me i have to try 10 times and with somethings it just doesn't work like this one idk what am i doing wrong
Great question! You don't need a temp file, you could simply store all data in some form of an array, however, that is harder to scale upwards for gigantic files as it will be far more RAM consuming. This method also demonstrates A-Level computer science theory, which was what my channel focused on at the time of creating this video.
Can it work on .csv file? This is my question on stackoverflow : stackoverflow.com/questions/62588154/delete-item-from-csv-file-using-java I hope u can help me
This tutorial can work for a csv file, it will work for any file type that uses commas to separate values. I won't post an answer as this tutorial does what you are looking for.
Just a little note, at 1:19 the record containing '2331,yoyo,56' isn't meant to be written to temp.txt until after we have found the record we wish to delete (1:33), so I am sorry for any confusion my little editing error made.
Got it working! Just take note that save the text file using UTF-8 encoding or else this won't work.
What issues were you having?
I've literally copied the whole code of yours and the IO exception returns null. Dunno why, the only difference is that I have more String variables and use semicolons instead of commas as separators between the fields. (Yes I modified your code respectively). If you could help me, I would be grateful.
The approach was quite workable. Seemingly this approach allows to get away with minimal number of imported Java libraries and without importing "java.nio" library (Java's new input/output library). I worked out this approach on Java 8_update144. Code worked.
Many thanks for that feedback!
First of all great explanation understood what you were explaining, but im having an issue i'm the delete file line keeps returning false
i have ensured that all printwriters are closed and also check the text file permissions but still having difficulty deleting the file.
Do you have any idea why this is happening?
Thanks for saving my grades
Hi Max, why doesn't this code work when you call it from another class?It writes to temp file fine, but it doesnt rename temp file to the old file name.Thanks !
same !
May I see your code?
@@maxodidily i have the same problem, insert exactly the same code as urs
Max, i faced the same problem. Please help me
After
pw.flush();
pw.close();
Add:
bw.close();
fw.close();
Could you do a video that we can remove record with button and we can choose visually
Your the best coding teacher brother!!! Way better than my college teacher thanks a lot!!!
Love from India 🇮🇳😍
can we write new text in temp.txt and transfer this text to the old tutorial.txt ?
You could if you wanted to.
WHERE HAVE YOU BEEN MAN! I LOVE THE VIDEO. Subscribe and will refer to my friends. So fun watching your tutorial man. Thank you.
Hi Max, you mention 1 on 1 help and i was wondeirng if i can request some help with editing file records.
9:37 did you take a stroke? Appreciate the tut
well i have a printwriter and i want after it executes once the next time it executes to change a few lines of of it or to simply delete the whole file so it can be re written when i execute. is this even possible?
when I do this, the new file created at the end has a empty line between each record. Any ideas on how to fix that?
change println to printf. add "
" in the end of the statement to solve this issue
@@mervynfong9076 Hi do u mean like this pw.printf(title + "|" + author + "|" + genre + "|" + section + "
");
I did exactly every single thing and yet there's still an error, what's wrong with java hayst
I need help on a project that deals with a client/server where I will need to read/add/remove/edit a csv file. These videos have been good you think you can help me out on this with a video.
temp.txt file not overwriting while making a object of the class and call the removeRecord method
How do you delete a record that the user wants to delete? The user inputs what they want to delete?
What would you add to this code?
Great video btw!
Scanner getInput= new Scanner(System.in); String id= getInput.nextInt(); Something like this could work. This goes at the top of the main function.
Thanks! Here is a tutorial I made on getting user input: ruclips.net/video/rioHqpj5xWo/видео.html What exactly isn't working?
Great Video my guy. This helped me tremendously.
[Please read it, although it's quite damn long!]
Hi! I have a problem. Every very first time I substitute the file for temp.txt (either deleting or editing the file), NetBeans becomes literally unable to read anything which concerns the last item in the record (in this case age) in the other methods that read from edited file. Even though, I put e.g. System.print.out.ln("age:" + age) between x.next lines and if condition in the while (x hasNext()) loop, it just prints the blank line, whereas all other variables, such as ID and name are printed! What is interesting, the NetBeans is able to write the last item (age) to the arrayList, though, when I am running my program.
Nevertheless, I am unable to make any comparisons (editions and deletiins as well) of anything that concerns the file with once made temp-procedure, as any comparisons with blank value of the last item of each record cannot be made. Max (or anybody else) please help! Thanks in advance!
I'm sorry you are having this issue, could you send your code to me via hastebin?
I have a problem, where I have set the deleteRecord as a class and I would like to call it in a main function. However I have encountered a problem that the when I wanted to call the removeRecord() with the argument of filepath and removeTerm , it is not working.
Could you define specifically isn't working?
@@maxodidily Firstly, I would like to apologize for the super duper late response :/ but I managed to fixed it due to my stupid mistakes. It's just that I called two of the methods in one class, where the first method has already called the second method. So my problem has been resolved. Also a huge thanks to you for providing this tutorial! I really really do appreciate it and keep doing what you like Max! :D
Very helpful Thanks mn
Coding + Joke but way to teach. Thank you so much sir!
Best*
love it, thx man!
Thanks a lot for saying you loved it, it really means a lot.
,hi sir.. can you help me on my assignment.. How to delete a record by asking what is the id first..?
Is there a possible way to call this class to a jframe ??
Why you write public static Scanner x and you can use any word can you tell us why because Scanner mean to get information from the user
You can also use the Scanner object to read files. The Scanner object reads inputs, a file counts as a source for inputs of data.
This program is for delete line when data is found on the first split, but didn't do anything if data found after the second and last split. Please teach us to do is data found in last split.
Your help is very appreciated.
You can easily modify this code to check any field of a record. 'x.next' reads a field of a record, I use 'x.next' multiple times to read all fields of a record. In this tutorial we check the first field of a record (the first 'x.next') but you can easily just compare any 'x.next' to the deleteTerm variable.
@@maxodidily i've tried this but still not working.
public class test {
static Scanner x;
public static void main(String [] args) {
String filepath = "input.txt";
String removeterm = "101";
removeRecord(filepath,removeterm);
}
private static void removeRecord(String filepath, String removeterm) {
String tempFile = "temp.txt";
File oldfile = new File(filepath);
File newfile = new File(tempFile);
String ID1 = ""; String ID2 = ""; String ID3 = "";
String ID4 = ""; String ID5 = ""; String ID6 = "";
String ID7 = ""; String ID8 = ""; String ID9 = "";
String ID10 = ""; String ID11 = ""; String ID12 = "";
String ID13 = ""; String ID14 = ""; String ID15 = "";
String ID16 = ""; String ID17 = ""; String ID18 = "";
String ID19 = ""; String ID20 = ""; String ID21 = "";
String ID22 = ""; String ID23 = ""; String ID24 = "";
String ID25 = "";
try
{
FileWriter fw = new FileWriter(tempFile,true);
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
x = new Scanner(new File(filepath));
x.useDelimiter("[~
]");
while(x.hasNext())
{
ID1 = x.next(); ID2 = x.next(); ID3 = x.next();
ID4 = x.next(); ID5 = x.next(); ID6 = x.next();
ID7 = x.next(); ID8 = x.next(); ID9 = x.next();
ID10 = x.next(); ID11 = x.next(); ID12 = x.next();
ID13 = x.next(); ID14 = x.next(); ID15 = x.next();
ID16 = x.next(); ID17 = x.next(); ID18 = x.next();
ID19 = x.next(); ID20 = x.next(); ID21 = x.next();
ID22 = x.next(); ID23 = x.next(); ID24 = x.next();
ID25 = x.next();
if(!ID25.equals(removeterm)) {
pw.println(ID1 + "~" + ID2 + "~" + ID3 + "~" + ID4 + "~" + ID5 + "~" + ID6 + "~" + ID7 + "~" + ID8 + "~" + ID9 + "~" + ID10 + "~" + ID11 + "~" + ID12 + "~" + ID13 + "~" + ID14 + "~" + ID15 + "~" + ID16 + "~" + ID17 + "~" + ID18 + "~" + ID19 + "~" + ID20 + "~" + ID21 + "~" + ID22 + "~" + ID23 + "~" + ID24 + "~" + ID25);
JOptionPane.showMessageDialog(null, "Done !");
}
}
x.close();
pw.flush();
pw.close();
oldfile.delete();
File dump = new File(filepath);
newfile.renameTo(dump);
}
catch (NoSuchElementException exception) {
// Output expected NoSuchElementExceptions.
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null, "Error !" + e);
}
}
}
Sir, how to make it work with a .csv extension? It's working with txt but not working with csv extension. Please revert asap its required for a project
Can you describe what happens when you run your code?
your the great !!
Where it says if(ID.equals(removeTerm))
I have an error stating that it cannot find symbol??
Could you send your code via hastebin please?
@@maxodidily what's hastebin sorry?
hastebin.com/egilobibot.json A great way to quickly send code to someone.
@@maxodidily sorry only just seen this, i have saved my code there
@@maxodidily hastebin.com/podepasubu.js
Hi, it works totally fine for me, but what if I want to delete this item several times? But, i don't want to change the filepath name. Thank you!
What do you mean by delete this item several times?
@@maxodidily oh, i want to delete another item also. Is it possible?
For example, i want to delete 2331 also
@@shellywu4945 Thanks for clarifying, you would have to pass in multiple deleteTerms into the fucntion, either as multiple strings or as a string array. I would advise a String array, call it String[] deleteTermsArray and instead of 'If(!ID.equals(deleteTerm))' you could do 'If(!Arrays.asList(deleteTermsArray).contains(ID))'. Hope this helps! You could also use an arraylist instead of a String array, I have a tutorial series on arraylists here: ruclips.net/video/TCsNrGKC340/видео.html
@@maxodidily I'll try thanks
It amazes me how nothing works smoothly with me i have to try 10 times and with somethings it just doesn't work like this one idk what am i doing wrong
Could you share your code?
Gr8
Thank you!
I'm glad you enjoyed!
great vid
Why do we have to make a new file why cant we just delete a line in the file
Great question! You don't need a temp file, you could simply store all data in some form of an array, however, that is harder to scale upwards for gigantic files as it will be far more RAM consuming. This method also demonstrates A-Level computer science theory, which was what my channel focused on at the time of creating this video.
@@maxodidily Can u show it for an Array ?
not working
how about now?
Can it work on .csv file? This is my question on stackoverflow :
stackoverflow.com/questions/62588154/delete-item-from-csv-file-using-java
I hope u can help me
This tutorial can work for a csv file, it will work for any file type that uses commas to separate values. I won't post an answer as this tutorial does what you are looking for.