Edit a Record in a CSV/txt File Java

แชร์
ฝัง
  • เผยแพร่เมื่อ 13 ต.ค. 2024
  • Edit a Record in a CSV/txt file Java
    Hello there viewer, welcome back and today I shall be showing you a simple way to edit records using Java for a txt or CSV or any file type. All we do is write every record we want to keep the same to another file and the new details for the record we want to edit to the new file. We delete the old file and rename the new file to whatever the old one was called.
    Why not subscribe to keep notified when I upload so you can get that A?
    tinyurl.com/Su...
    Social media:
    Instagram: / mcod1999
    Twitter: / mcod1999
    ManDownGG: www.mandown.pro/ - Le pro Esport team
    Edit a Record in a CSV/txt file Java

ความคิดเห็น • 146

  • @matthewkarloski4775
    @matthewkarloski4775 2 ปีที่แล้ว +11

    For people having to edit this file more than once, doing it with pw.println(...) causes the ending of each line to have "
    ". Meaning that whenever you go through it again, your last element on that line will have a "
    " at the end, causing a new line to appear, and also causing errors if you're trying to use that variable as something besides a string, like a int.
    In this case, age would be "36
    ", "23
    ", or whatever you gave age, instead of just the number.
    There's a simple solution however. Simply use pw.print(... + "
    "). This eliminates the "
    " and solves all the problems I listed above.
    So if we're using the example from the video, it'd be:
    pw.print(ID + "," + name + "," + age + "
    ");

    • @oFancys
      @oFancys ปีที่แล้ว

      ooh, merci!

  • @ishanvarshney9054
    @ishanvarshney9054 6 ปีที่แล้ว +11

    OMG ,I can't believe such a great video has only 15 likes! and this wonderful channel has only 99 subscribers. Keep going,
    I am sure u would cross 1M subscribers one day.

    • @maxodidily
      @maxodidily  6 ปีที่แล้ว +3

      Thanks a lot, Ishan. You will have no idea how much it means to me hearing those words. Also, we are now at 100 subs now so we just need to cube that number to get to one million now. I hope you stay tuned for more!

    • @m7mdabohashem
      @m7mdabohashem 3 ปีที่แล้ว

      it's 370 likes now

    • @user-mw1qb5kd9l
      @user-mw1qb5kd9l 3 ปีที่แล้ว +1

      he's not that close yet, but surely someday...

    • @SachinPatil-vj1hu
      @SachinPatil-vj1hu 3 ปีที่แล้ว

      @@maxodidily 3000 Subs now!

  • @qkloh6804
    @qkloh6804 3 ปีที่แล้ว +2

    A video that can help me after 2 years now and more years to come to help others. Great work.

  • @fangzsx0409
    @fangzsx0409 4 ปีที่แล้ว +4

    May 14 2020, your tutorials are very informative and on point. This is the only tutorial that worked for me. Thanks Max

  • @mamlesksiddharth
    @mamlesksiddharth 3 ปีที่แล้ว +2

    Hey Max! This is such an awesome video! I just wanted to know if it was possible to edit just a single cell rather than changing the entire row each time I want to edit. Thanks!!!

  • @SebgasFlowx3
    @SebgasFlowx3 4 ปีที่แล้ว +3

    Hey man, great video, i followed everything in order to do my code, but since im using a csv file, somehow the file is not being deleted nor changed, i don't know if the swing affects something. the temp.csv is being created correctly but the old file isn't being deleted and the temp isn't being renamed, i don't know if you will ever read this but yolo. Thanks for the video

  • @TiMCoop3r
    @TiMCoop3r ปีที่แล้ว

    what would be your solution if one of the items( x.next()) is a double but the rest are a string?

  • @thomas.l5924
    @thomas.l5924 ปีที่แล้ว

    Thank you very much you save my life :D .

  • @tuantng425
    @tuantng425 4 ปีที่แล้ว +1

    I have troubles deleting the old file and renaming the new file as File.delete() and File.renameTo() are ignored. The code works GREAT (THANK YOU SO MUCH!) but it does not delete or rename the file. Again, thank you for your helpful videos!

    • @TAIGZZZZ
      @TAIGZZZZ 3 ปีที่แล้ว

      same problem here

    • @jeisonfonseca4030
      @jeisonfonseca4030 3 ปีที่แล้ว

      Did you know how to solving?

  • @alainsoral7314
    @alainsoral7314 5 ปีที่แล้ว

    Hello, it the thumbs up guy again. I have a question,
    isn't this ressources consuming if it a dictionary file for example ? not to mention if the file is also constantly modified by other methods !?

    • @maxodidily
      @maxodidily  5 ปีที่แล้ว +2

      Good question, so yes, this can be resource demanding if used in a big database system. For small databases, this is fine. This tutorial was aimed at A-Level students and this is enough for A Levels. If I were to code a bigger database I would use hashing. I shall be covering this in the near future.

    • @alainsoral7314
      @alainsoral7314 5 ปีที่แล้ว

      @@maxodidily thank you, looking forward for the hashing tutorial. keep the good work

  • @rajojyoti
    @rajojyoti 4 ปีที่แล้ว +1

    Hi Max,
    This is a awesome video.
    I have one ques?
    How can I use for a csv file and not a text file

    • @maxodidily
      @maxodidily  4 ปีที่แล้ว

      Change the filename from "filename.txt" to "filename.csv".

    • @rajojyoti
      @rajojyoti 4 ปีที่แล้ว

      @@maxodidily Thanks max.
      I will try and will let you know.
      Anyways, the session was very helpfull to me.
      Thanks man

    • @Sheoranhe
      @Sheoranhe 3 ปีที่แล้ว

      Is it possible without changing? i mean can't we directly edit the csv file? I am looking to add a new record in an already existing csv file.

  • @mohsenmcqueen
    @mohsenmcqueen 6 ปีที่แล้ว +2

    Thx man, from iran with love :) whish you the best

    • @maxodidily
      @maxodidily  6 ปีที่แล้ว +1

      Your words make my day! I wish you the best too.

  • @manjunathmali5310
    @manjunathmali5310 5 ปีที่แล้ว +1

    Really helpful bro thanks a lot ....

  • @Total_FootballYT
    @Total_FootballYT 3 ปีที่แล้ว

    Hi mate, I’m getting NoSuchElementException with this and can’t figure out why, any ideas?

  • @sirtroy
    @sirtroy 4 ปีที่แล้ว

    ARE YOU DRUNK?!
    But thanks, I learned

  • @data_science_uzbekistan
    @data_science_uzbekistan 9 วันที่ผ่านมา

    Thanks !

  • @devishamishra7279
    @devishamishra7279 ปีที่แล้ว

    How to keep the alignment same as old file..plz answer

  • @Jacksons_are_jackson
    @Jacksons_are_jackson 2 ปีที่แล้ว

    Thanks dude. U da best

  • @NTChannel127
    @NTChannel127 3 ปีที่แล้ว

    thanks so much for sharing, it's really useful

  • @parmjitgill592
    @parmjitgill592 4 ปีที่แล้ว

    Hi Max, great video. I'm just having a little trouble when the program loops through the code to find and edit the desired values. It finds and changes the values fine but the program doesn't write the remaining values in the file (the other rows)

    • @Sheoranhe
      @Sheoranhe 3 ปีที่แล้ว

      I am looking to add a new record in an already existing csv file what will be the logic ? And Input from console.

  • @tafroggo5813
    @tafroggo5813 5 ปีที่แล้ว +1

    wait, why would you make a temporary file when you could just add the stuff to A string array?
    that way it would be a lot easier...

    • @maxodidily
      @maxodidily  5 ปีที่แล้ว +1

      You can use an Array if you like. I used a temp file as I wanted to keep this video in line with the WJEC Computer Science A-Level syllabus where the theory states a temp file is created, the old file is deleted and the temp file is renamed to the original file.

  • @iloveyoueggegg
    @iloveyoueggegg 4 ปีที่แล้ว

    How do you stop it from starting a new line in the text file after you've written what you've written?

  • @TAIGZZZZ
    @TAIGZZZZ 3 ปีที่แล้ว +1

    it runs but but it doesnt delete the temp file and doesnt update file

  • @junkai6877
    @junkai6877 3 ปีที่แล้ว

    Hi there, may I ask why mine will keep adding a blank line in eachline?

  • @tingkoydesu1837
    @tingkoydesu1837 4 ปีที่แล้ว +5

    File.renameTo()
    and file.delete() is not working for meee

    • @amannazarri5182
      @amannazarri5182 3 ปีที่แล้ว

      same here. so how to solve? any ideas?

  • @gerhardkotze5934
    @gerhardkotze5934 3 ปีที่แล้ว

    This is great code, but my problem is I can't just use Strings. What if you use different data types?

  • @shoot-em-up895
    @shoot-em-up895 4 ปีที่แล้ว

    You are great man

  • @-0-__-0-
    @-0-__-0- 2 ปีที่แล้ว

    Having issues with file.delete() returning false. Any way to fix it?

  • @jackpang9909
    @jackpang9909 5 ปีที่แล้ว +1

    Thanks bro, it is useful. But can i know why the rename and delete function are not working by using the same code with you?
    the main issue is cannot delete or rename the Main text file. So temporary file will not replace the Main file.

    • @yapkarman7431
      @yapkarman7431 5 ปีที่แล้ว

      do u found solution for this? I'm having the same problem. data is copied to temp.txt but didn't rename to records.txt

    • @maxodidily
      @maxodidily  5 ปีที่แล้ว

      Could you send your code via hastebin?

    • @jackpang9909
      @jackpang9909 5 ปีที่แล้ว

      Thank you very much for replying my comment
      But i found a way which is overwrite the original file from temporary file by using the same method above
      It should be work also

    • @maxodidily
      @maxodidily  5 ปีที่แล้ว

      @@jackpang9909 I'm glad you have resolved the issue. If you have any other issues, don't hesitate to ask about it!

    • @isore3090
      @isore3090 5 ปีที่แล้ว

      @@maxodidily hello bro, im havinf the same problem as JACK PANG and i couldnt find a solution. Can you help me with it?

  • @mohammedf.sammour8318
    @mohammedf.sammour8318 5 ปีที่แล้ว

    Thank you very much for this good, beautiful professional work

    • @maxodidily
      @maxodidily  5 ปีที่แล้ว

      Your kind words have made my day.

    • @mohammedf.sammour8318
      @mohammedf.sammour8318 5 ปีที่แล้ว

      @@maxodidily It is my pleasure to thank everyone who teaches and helps people .. I wish you happy times

  • @ayanc13
    @ayanc13 6 ปีที่แล้ว

    Using a windows and linux machine both, this code works for me in linux perfectly. but in windows the file does not delete at any condition!! :( can you teach me any other method?

    • @maxodidily
      @maxodidily  6 ปีที่แล้ว

      Firstly, I am really happy that the code works on Linux and am sorry it isn't working on Windows. Here is something you may want to have a look at, it aims to accomplish a similar thing: stackoverflow.com/questions/20039980/java-replace-line-in-text-file Also may I see the code so I can see if it works on my Windows machine?

    • @abhishekjha6487
      @abhishekjha6487 6 ปีที่แล้ว

      for windows try to use java.nio.file.Files.delete(oldfile.toPath());

  • @8-bitJoe
    @8-bitJoe 4 ปีที่แล้ว

    Hi, everything seems to run fine except every time I run the program, it keeps creating a new line empty space on the file. I've copied your code and it's still happening? Do you know what's causing this? Thanks.

    • @brentgallagher3603
      @brentgallagher3603 4 ปีที่แล้ว +3

      if(item.equals(editID)) {

      pw.print(newID + "," + newName + "," + newAge + "
      ");
      }

      else {
      pw.print(ID+ "," + name + "," + age + "
      ");

      }
      use pw.print not pw.println and add a "
      " in the print statement...Answer is above

    • @rickywong9525
      @rickywong9525 3 ปีที่แล้ว

      @@brentgallagher3603 Oh my gosh, thanks!! Was looking for the solution to this problem, and your answer helps me!

  • @adolfjamesurian6510
    @adolfjamesurian6510 4 ปีที่แล้ว

    What if i want to append something in a line of records?

  • @pushkargoyal4278
    @pushkargoyal4278 4 ปีที่แล้ว

    nyc explanation

  • @mmarvinn__
    @mmarvinn__ 3 ปีที่แล้ว

    Does this work with ";" instead of "," too?

  • @YourDhiraj
    @YourDhiraj 4 ปีที่แล้ว

    SIr, i wrote the code but not working properly. No error message is being displayed. if you don't mind, could you give me any suggestion.

    • @maxodidily
      @maxodidily  4 ปีที่แล้ว

      Could you send the code via hastebin or pastebin please

  • @catalinsas2390
    @catalinsas2390 3 ปีที่แล้ว

    my file is not deleted and the updated data remains in temp fil....could u please help????

  • @mrthaw4962
    @mrthaw4962 6 ปีที่แล้ว

    If i do not close all my filereader and filewriter will that stop the file from deleting and renaming ?

    • @maxodidily
      @maxodidily  6 ปีที่แล้ว +1

      I'll be honest, I'm not 100% sure on that, I just close everything as it is good practice and reduces resources used. Maybe try your code but without closing the filereader and filewriter. I'm sorry I couldn't give u an answer to this good question.

  • @Sheoranhe
    @Sheoranhe 3 ปีที่แล้ว

    I am looking to add a new record in an already existing csv file what will be the logic ? And Input from console.

    • @maxodidily
      @maxodidily  3 ปีที่แล้ว

      This tutorial will show you how to add a record to a csv file: th-cam.com/video/lp0xQXUEw-k/w-d-xo.html As for getting input, use the Java Scanner class and assign input it receives to a string.

  • @internettboii_
    @internettboii_ 6 ปีที่แล้ว

    Nice video bro, help me a Lot.
    How can I copy from text file to object type arraylist?
    Like 1223,Bilton,23 to 1223 Billton 23

    • @Gerrieberrie76
      @Gerrieberrie76 5 ปีที่แล้ว

      You go through the txt file, putting every line in a String (use a String[ ] for that), and then use the String.split( " , " ) to split the lines at the comma.

  • @shoot-em-up895
    @shoot-em-up895 4 ปีที่แล้ว

    Thank you very much

  • @bellthomas537
    @bellthomas537 7 ปีที่แล้ว

    simple i love it

    • @maxodidily
      @maxodidily  7 ปีที่แล้ว

      Thanks for the positive feedback!

  • @kylemital689
    @kylemital689 5 ปีที่แล้ว

    I tried the code, but instead of setting the new information to a set amount, I made a scanner that allowed the user to input what they want to change and then that was inputed into the newID for example. It doesn't work. Any help?

    • @maxodidily
      @maxodidily  5 ปีที่แล้ว

      Could you send me the code via hastebin or pastebin please?

    • @Sheoranhe
      @Sheoranhe 3 ปีที่แล้ว

      I am looking to add a new record in an already existing csv file what will be the logic ? And also Input should be from console.

    • @yazeedmizu3184
      @yazeedmizu3184 3 ปีที่แล้ว

      That's what I tried to do but did not work. Do you still have your coding for it?

  • @MrEvmk
    @MrEvmk 4 ปีที่แล้ว

    Hi
    Max O'Didily , If I created a CSV file how to create header for that CSV.

    • @maxodidily
      @maxodidily  4 ปีที่แล้ว

      Just add a record to the top of the file which just consists of headers fur your fields.

  • @seihaly8375
    @seihaly8375 5 ปีที่แล้ว +1

    Hello,I have follow your code and i have problem with update data. When i update it.The "oldFile" not delete and "Temp file" is also not rename too. Can you guide me please?. Thanks.

    • @syamhans12
      @syamhans12 4 ปีที่แล้ว

      same.

    • @ahmednasif5443
      @ahmednasif5443 4 ปีที่แล้ว

      Same here

    • @Sheoranhe
      @Sheoranhe 3 ปีที่แล้ว

      I am looking to add a new record in an already existing csv file what will be the logic ? And Input from console.

  • @yq5687
    @yq5687 4 ปีที่แล้ว

    i cannot delete the file when export file and import file

  • @Johncena-bk1lg
    @Johncena-bk1lg 6 ปีที่แล้ว

    You deserve my sub!!!

    • @maxodidily
      @maxodidily  6 ปีที่แล้ว

      Ty, it feels good knowing I earnt a subscription!

  • @syamhans12
    @syamhans12 4 ปีที่แล้ว

    Max, help me.. The old file not deleting , therefore at the end of the project i have 2 different file exist

    • @maxodidily
      @maxodidily  4 ปีที่แล้ว

      Could you send me your code via hastebin or pastebin please.

  • @practifruta7330
    @practifruta7330 3 ปีที่แล้ว

    gracias amigo

  • @НикифорБогданов
    @НикифорБогданов 5 ปีที่แล้ว

    Nevermind. Nice video!

    • @maxodidily
      @maxodidily  5 ปีที่แล้ว

      Could you send me your code via hastebin?

    • @НикифорБогданов
      @НикифорБогданов 5 ปีที่แล้ว

      @@maxodidily I wrote the data without comas so it didn't write anything

    • @maxodidily
      @maxodidily  5 ปีที่แล้ว

      ​@@НикифорБогданов I'm glad you resolved your issue!

  • @ritikarao1662
    @ritikarao1662 5 ปีที่แล้ว

    sir, thsi code is working gud for txt bt i gotta edit the csv file ..it shows no changes in that..please help

    • @maxodidily
      @maxodidily  5 ปีที่แล้ว

      There should be no issues with this code being used on a .csv file. Could you send your code via hastebin, please?

    • @Sheoranhe
      @Sheoranhe 3 ปีที่แล้ว

      I am looking to add a new record in an already existing csv file what will be the logic ? And Input should be from console.

  • @adityaprerepa7656
    @adityaprerepa7656 6 ปีที่แล้ว +1

    Dont you need to do Scanner x = new Scanner(new File(filepath));

    • @maxodidily
      @maxodidily  6 ปีที่แล้ว +3

      That way also works just as well.

  • @suswee_8189
    @suswee_8189 3 ปีที่แล้ว

    thanks

  • @igrey9616
    @igrey9616 3 ปีที่แล้ว

    why do we use this as delimiter [,
    ] i don't understand that concept

    • @maxodidily
      @maxodidily  3 ปีที่แล้ว

      The comma is because we want every field to be separated by a comma, we also use
      as that means a new line.

  • @kevinpaulsen6103
    @kevinpaulsen6103 6 ปีที่แล้ว

    Curly brackets don’t go on a new line, and there’s always a space after a comma.

    • @meep1543
      @meep1543 6 ปีที่แล้ว

      but they do though

    • @kevinpaulsen6103
      @kevinpaulsen6103 6 ปีที่แล้ว

      meep not according to official java style. If you put them on a new line at a good college, you’d get points marked off. Java has a code style, you can’t change it with you bad habits.

  • @roselynw1872
    @roselynw1872 6 ปีที่แล้ว

    what if you only want to change the name and the rest are the same? :(

    • @maxodidily
      @maxodidily  6 ปีที่แล้ว

      If you only want to change one field, you could do 'pw.println(ID + "," + newName + "," + age);' Basically, if you ever want to keep something the same you just re-write the value that existed in the original file.

  • @mahinzafar4545
    @mahinzafar4545 5 ปีที่แล้ว

    could this code work in an android application in android studio

  • @sushomamondal6950
    @sushomamondal6950 4 ปีที่แล้ว

    Sir after running this code in my system it is showing error..... i can't understand what is the Exact problem....Sir plzz tell me the solution. Thanks in advance.

    • @maxodidily
      @maxodidily  4 ปีที่แล้ว

      What is the error message you are receiving?

    • @sushomamondal6950
      @sushomamondal6950 4 ปีที่แล้ว

      @@maxodidily Actually sir catch block is executing...And it is printing error.

    • @maxodidily
      @maxodidily  4 ปีที่แล้ว

      Could you send me your code please?

    • @Sheoranhe
      @Sheoranhe 3 ปีที่แล้ว

      I am looking to add a new record in an already existing csv file what will be the logic ? And Input should be from console.

  • @sithchanrady8125
    @sithchanrady8125 5 ปีที่แล้ว

    good

  • @dariusboteand7722
    @dariusboteand7722 5 ปีที่แล้ว

    FileWriter(blablabla) is underlined in red... only solution proposed: create a new class FileWriter... why?

    • @maxodidily
      @maxodidily  5 ปีที่แล้ว

      Could you send me your code via hastebin or pastebin please?

  • @魅力之王
    @魅力之王 3 ปีที่แล้ว

    the link is not available, plz update

  • @matthewsilverberg6618
    @matthewsilverberg6618 7 ปีที่แล้ว

    WOW

  • @waltermonteiro7078
    @waltermonteiro7078 5 ปีที่แล้ว

    if your editTerm is the age it will not change the record

    • @maxodidily
      @maxodidily  5 ปีที่แล้ว

      Could you send your code via hastebin please?

    • @waltermonteiro7078
      @waltermonteiro7078 5 ปีที่แล้ว

      problem solved sorry for bothering

    • @maxodidily
      @maxodidily  5 ปีที่แล้ว

      No problem, it's never a bother if anyone asks a question. May I ask how you resolved your issue?

  • @killerzero8516
    @killerzero8516 3 ปีที่แล้ว

    why is it printing unorganized

  • @ujjawalyadav574
    @ujjawalyadav574 4 ปีที่แล้ว

    Sir nosuchelementexception occur why

    • @maxodidily
      @maxodidily  4 ปีที่แล้ว +1

      Could you send your code via hastebin or pastebin please

    • @soumyabej5770
      @soumyabej5770 4 ปีที่แล้ว

      @@maxodidily sir we don't know how to use

    • @maxodidily
      @maxodidily  4 ปีที่แล้ว

      Paste your code into the giant text box here: pastebin.com/ After that, click the 'create new paste' button. After your are loaded to a new page, copy what is in the url bar on your web browser and paste the link wherever you wish to share your pastebin link.

    • @soumyabej5770
      @soumyabej5770 4 ปีที่แล้ว

      @@maxodidily ok sir

    • @soumyabej5770
      @soumyabej5770 4 ปีที่แล้ว

      @@maxodidily pastebin.com/zRSLwavW here it is sir

  • @dariusboteand7722
    @dariusboteand7722 5 ปีที่แล้ว

    also code doesn't work :")

    • @maxodidily
      @maxodidily  5 ปีที่แล้ว

      What issue have you encountered?

  • @muzilhassan2542
    @muzilhassan2542 4 ปีที่แล้ว

    this is not working

    • @maxodidily
      @maxodidily  4 ปีที่แล้ว

      What issue are you having?

  • @jimmyinnocent9884
    @jimmyinnocent9884 ปีที่แล้ว

    CAN SOMEONE PLEASE HELP, I DID EXACTLY AS THE VIDEO BUT IT SHOWS java.util.NoSuchElementException
    BTW GREAT VIDEOS MAN, HELPED ALOT
    public static void editRecord(String filepath, String editTerm, String newFname, String newSname, String newEmail,String newAddress, String newPhone)
    {
    String tempFile = "temp.txt";
    File oldFile = new File(filepath);
    File newFile = new File(tempFile);
    String fname = ""; String sname = ""; String email = ""; String address = ""; String phone = "";
    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("[\t
    ]");
    while(x.hasNext()){
    fname = x.next();
    sname = x.next();
    email = x.next();
    address = x.next();
    phone = x.next();
    if(fname.equals(editTerm)){
    pw.println(newFname + "\t" + newSname + "\t" + newEmail + "\t" + newAddress + "\t" + newPhone);
    }else{
    pw.println(fname + "\t" + sname + "\t" + email + "\t" + address + "\t" + phone);
    }
    }
    x.close();
    pw.flush();
    pw.close();
    oldFile.delete();
    File dump = new File(filepath);
    newFile.renameTo(dump);
    }catch(Exception e){
    System.out.println(e);
    }
    }