Deleting a record in a file in Java

Поделиться
HTML-код
  • Опубликовано: 2 дек 2024

Комментарии • 95

  • @maxodidily
    @maxodidily  7 лет назад +3

    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.

  • @REMZEJ
    @REMZEJ 6 лет назад

    Got it working! Just take note that save the text file using UTF-8 encoding or else this won't work.

    • @maxodidily
      @maxodidily  6 лет назад

      What issues were you having?

  • @rafalk35
    @rafalk35 5 лет назад +2

    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.

  • @Intars5d
    @Intars5d 6 лет назад

    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.

    • @maxodidily
      @maxodidily  6 лет назад

      Many thanks for that feedback!

  • @Incognitooe
    @Incognitooe 5 лет назад +1

    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?

  • @Jacksons_are_jackson
    @Jacksons_are_jackson 2 года назад

    Thanks for saving my grades

  • @blessenvarghese4147
    @blessenvarghese4147 5 лет назад +3

    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 !

    • @basmasalem3190
      @basmasalem3190 5 лет назад

      same !

    • @maxodidily
      @maxodidily  5 лет назад

      May I see your code?

    • @kahhauchong565
      @kahhauchong565 4 года назад

      @@maxodidily i have the same problem, insert exactly the same code as urs

    • @syamhans12
      @syamhans12 4 года назад

      Max, i faced the same problem. Please help me

    • @scrotumboy
      @scrotumboy 4 года назад

      After
      pw.flush();
      pw.close();
      Add:
      bw.close();
      fw.close();

  • @mertmer1052
    @mertmer1052 3 года назад

    Could you do a video that we can remove record with button and we can choose visually

  • @gagantechtechnical6737
    @gagantechtechnical6737 4 года назад

    Your the best coding teacher brother!!! Way better than my college teacher thanks a lot!!!
    Love from India 🇮🇳😍

  • @dreamscapeai7
    @dreamscapeai7 5 лет назад +2

    can we write new text in temp.txt and transfer this text to the old tutorial.txt ?

    • @maxodidily
      @maxodidily  5 лет назад

      You could if you wanted to.

  • @francisgalo8324
    @francisgalo8324 4 года назад

    WHERE HAVE YOU BEEN MAN! I LOVE THE VIDEO. Subscribe and will refer to my friends. So fun watching your tutorial man. Thank you.

  • @siddhantmidha1017
    @siddhantmidha1017 Год назад

    Hi Max, you mention 1 on 1 help and i was wondeirng if i can request some help with editing file records.

  • @Randomname77
    @Randomname77 4 года назад +1

    9:37 did you take a stroke? Appreciate the tut

  • @renbll
    @renbll 6 лет назад

    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?

  • @cmac972
    @cmac972 5 лет назад +2

    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?

    • @mervynfong9076
      @mervynfong9076 4 года назад

      change println to printf. add "
      " in the end of the statement to solve this issue

    • @hongying3500
      @hongying3500 4 года назад

      @@mervynfong9076 Hi do u mean like this pw.printf(title + "|" + author + "|" + genre + "|" + section + "
      ");

  • @luisgoyone6003
    @luisgoyone6003 4 года назад +1

    I did exactly every single thing and yet there's still an error, what's wrong with java hayst

  • @MrIkeole
    @MrIkeole 4 года назад +1

    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.

  • @md.al-amin1650
    @md.al-amin1650 4 года назад

    temp.txt file not overwriting while making a object of the class and call the removeRecord method

  • @michealmclaughlin429
    @michealmclaughlin429 4 года назад

    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!

    • @maxodidily
      @maxodidily  4 года назад

      Scanner getInput= new Scanner(System.in); String id= getInput.nextInt(); Something like this could work. This goes at the top of the main function.

    • @maxodidily
      @maxodidily  2 года назад

      Thanks! Here is a tutorial I made on getting user input: ruclips.net/video/rioHqpj5xWo/видео.html What exactly isn't working?

  • @MrUnknown107
    @MrUnknown107 5 лет назад

    Great Video my guy. This helped me tremendously.

  • @szymongainski7233
    @szymongainski7233 5 лет назад

    [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!

    • @maxodidily
      @maxodidily  5 лет назад

      I'm sorry you are having this issue, could you send your code to me via hastebin?

  • @MrBlurrBoii
    @MrBlurrBoii 6 лет назад

    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
      @maxodidily  6 лет назад +1

      Could you define specifically isn't working?

    • @MrBlurrBoii
      @MrBlurrBoii 6 лет назад

      @@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

  • @jaylordjl6337
    @jaylordjl6337 2 года назад

    Very helpful Thanks mn

  • @shaikhsanah3087
    @shaikhsanah3087 4 года назад +1

    Coding + Joke but way to teach. Thank you so much sir!

  • @ben8140
    @ben8140 6 лет назад +1

    love it, thx man!

    • @maxodidily
      @maxodidily  6 лет назад

      Thanks a lot for saying you loved it, it really means a lot.

  • @gabyaguilar741
    @gabyaguilar741 5 лет назад

    ,hi sir.. can you help me on my assignment.. How to delete a record by asking what is the id first..?

  • @fgg102
    @fgg102 5 лет назад

    Is there a possible way to call this class to a jframe ??

  • @salmana7589
    @salmana7589 5 лет назад

    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

    • @maxodidily
      @maxodidily  5 лет назад

      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.

  • @akifzak245
    @akifzak245 5 лет назад

    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.

    • @maxodidily
      @maxodidily  5 лет назад

      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.

    • @akifzak245
      @akifzak245 5 лет назад

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

  • @rajansharma2167
    @rajansharma2167 5 лет назад

    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

    • @maxodidily
      @maxodidily  5 лет назад

      Can you describe what happens when you run your code?

  • @davidbhondave9598
    @davidbhondave9598 3 года назад

    your the great !!

  • @khalidaslam1772
    @khalidaslam1772 6 лет назад

    Where it says if(ID.equals(removeTerm))
    I have an error stating that it cannot find symbol??

    • @maxodidily
      @maxodidily  6 лет назад

      Could you send your code via hastebin please?

    • @khalidaslam1772
      @khalidaslam1772 6 лет назад

      @@maxodidily what's hastebin sorry?

    • @maxodidily
      @maxodidily  6 лет назад

      hastebin.com/egilobibot.json A great way to quickly send code to someone.

    • @khalidaslam1772
      @khalidaslam1772 5 лет назад

      @@maxodidily sorry only just seen this, i have saved my code there

    • @khalidaslam1772
      @khalidaslam1772 5 лет назад

      @@maxodidily hastebin.com/podepasubu.js

  • @shellywu4945
    @shellywu4945 4 года назад

    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!

    • @maxodidily
      @maxodidily  4 года назад

      What do you mean by delete this item several times?

    • @shellywu4945
      @shellywu4945 4 года назад

      @@maxodidily oh, i want to delete another item also. Is it possible?

    • @shellywu4945
      @shellywu4945 4 года назад

      For example, i want to delete 2331 also

    • @maxodidily
      @maxodidily  4 года назад +1

      @@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

    • @shellywu4945
      @shellywu4945 4 года назад

      @@maxodidily I'll try thanks

  • @اريام-خ7ص
    @اريام-خ7ص 2 года назад

    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

    • @maxodidily
      @maxodidily  2 года назад

      Could you share your code?

  • @ishanvarshney9054
    @ishanvarshney9054 6 лет назад +3

    Gr8

  • @bitsplz
    @bitsplz 6 лет назад

    Thank you!

  • @drjfjfuvn6929
    @drjfjfuvn6929 6 лет назад

    great vid

  • @zzzzzzzjsjyue2175
    @zzzzzzzjsjyue2175 4 года назад

    Why do we have to make a new file why cant we just delete a line in the file

    • @maxodidily
      @maxodidily  4 года назад

      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.

    • @fxclan6297
      @fxclan6297 2 года назад

      @@maxodidily Can u show it for an Array ?

  • @limji-siang6496
    @limji-siang6496 5 лет назад +1

    not working

  • @jameslin7355
    @jameslin7355 4 года назад

    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

    • @maxodidily
      @maxodidily  4 года назад

      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.