Writing multiple lines with BufferedWriter in different try/catch statements

Discussion in 'Plugin Development' started by xpaintall, May 1, 2021 at 3:05 PM.

  1. Offline

    xpaintall

    Ok, so I have tried to store data into a .txt file, but it is not working for some reason. The BufferedWriter doesn't make new lines and it just overwrites the already existing data in it.
    Code:
    try{
                                                BufferedWriter fw = new BufferedWriter(new FileWriter(String.valueOf(p)));
                                                fw.newLine();
                                                fw.write("Super speed gadget: " + player.getName() +  "\n\r");
                                                fw.close();
                                            } catch (IOException e) {
                                             e.printStackTrace();
                                            }
    
    
    //different part of the code
    
    try{
                                                BufferedWriter fw = new BufferedWriter(new FileWriter(String.valueOf(p)));
                                                fw.newLine();
                                                fw.write("Super jump gadget: " + player.getName() + "\n\r");
                                                fw.close();
                                            } catch (IOException e) {
                                                e.printStackTrace();
                                            }
    please help!
     
  2. Offline

    davidclue

    You need append mode, just change your declaration to this
    Code:
    BufferedWriter fw = new BufferedWriter(new FileWriter(String.valueOf(p), true));
    And there is no need for newLine to be called so just remove that so your code looks like this
    Code:
    BufferedWriter fw = new BufferedWriter(new FileWriter(String.valueOf(p), true));
    fw.write("Super speed gadget: " + player.getName() +  "\n\r");
    fw.close();
     
  3. Offline

    xpaintall

    Thanks! It works how I wanted it to, but the problem now is that my scanner won't scan the file.

    Code:
    if (sc.next().matches("Super speed gadget: " +
    player.getName())) {
                    g.superspeed.add(player.getUniqueId());
              }
     
  4. Offline

    davidclue

    Get the string and print it out to see if it matches, and check what string it's reading.
     
  5. Offline

    xpaintall

    @davidclue I did it and I got a null message.
    If you don't understand what am I trying to achieve here, basically I have this file
    HTML:
    fortunecookie speed gadget: xpaintall
    
    crit trail: xpaintall
    
    Super jump gadget: xpaintall
    
    punch trail: xpaintall
    
    boom gadget: xpaintall
    
    Super speed gadget: xpaintall
    
    
    And I want to check if in the file a line of text exists (in my case: "boom gadget: " + player#getName())
    but if it doesn't, it is going to tell me: "not working". The problem is I cannot find out how to scan the entire file and try to find a specific line of text that says: "boom gadget: " + player#getName();
     
  6. Offline

    davidclue

    Post your entire scanning function.
     

Share This Page