Writing multiple lines with BufferedWriter in different try/catch statements

Discussion in 'Plugin Development' started by xpaintall, May 1, 2021.

  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.
     
  7. Offline

    xpaintall

    @davidclue what do you mean? Do you want me to post the code that I use to scan the file?
     
  8. Offline

    davidclue

  9. Offline

    xpaintall

    @davidclue ok, got the code. I am still learining lots about files in java so don't bully me if something is wrong.
    Code:
    try{
                Scanner sc = new Scanner(file);
                if(sc.nextLine().matches("boom gadget: " + player.getName())) {
                    player.sendMessage(ChatColor.RED + "It is working.");
                } else {
                    player.sendMessage(ChatColor.AQUA + "It isn't working.");
                }
            } catch(IOException e) {
                e.printStackTrace();
            }
    
     
  10. Offline

    davidclue

    Try this instead
    Code:
    public static boolean hasLine(String filePath, String text) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new FileReader(filePath));
            while (true) {
                String line = br.readLine();
                if (line == null) break;
                if (line.contains(text)) {
                    br.close();
                    return true;
                }
            }
            br.close();
            return false;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (br != null) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }
    And to call it just do
    Code:
    if (hasLine(filePath, "boom gadget: " + player.getName())) {
         player.sendMessage(ChatColor.RED + "It is working.");
    } else {
        player.sendMessage(ChatColor.AQUA + "It isn't working.");
    }
    Obviosuly there are more efficient ways of doing this but if you won't be calling it too much then it shouldn't be a problem to run on the main thread. Also the way I made it DO NOT leave any empty spaces between lines in the text file.
     
    Last edited: May 8, 2021

Share This Page