Results 1 to 2 of 2
  1. #1
    Squidkingdom is offline Member
    Join Date
    Apr 2017
    Rep Power

    Default BufferedReader and BufferedWriter

    am working on a IRC chat bot for a friend. My goal is to check a file (Ex: warn.txt) for any line contains the varible user if the username isnt in the file at all then i want to add a new line, while leaving all other lines the same, and have that name on it. For example the file would look like
    Java Code:
    and since that doesnt contain User3 i want to add it
    Java Code:
    I have the code:
    Java Code:
        public boolean checkWarnList(User user, Channel channel) throws IOException{
        Charset charset = Charset.forName("US-ASCII");
        Path path = Paths.get("D:\\Desktop\\warn.txt");
            BufferedReader reader = Files.newBufferedReader(path, charset);
            String line = null;
            boolean listed = false;
            while ((line = reader.readLine()) != null) {
               if(user.toString() == line){
                   sendMessage("You have been banned", channel);
                   listed = true;
               else {
                   try (BufferedWriter writer = Files.newBufferedWriter(path, charset)) {
                       writer.write(user.toString(), 0, user.toString().length());
                       sendMessage("You have been warned", channel);
                   } catch (IOException x) {
                       System.err.format("IOException: %s%n", x);
            return listed;
    Every time i run the function it always returns false and clears all of warn.txt. User and Channel are both valid variable types as well as the function
    Java Code:
    I know its something in the if statement but I can't figure out what. I will be the first to admit I am a SuperNoob when It comes to this part of Java. Any little hint would be nice. Thanks, Squidkingdom

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: BufferedReader and BufferedWriter

    To open a file for appending (which is what you want to do) you need ot use the version of newBufferedWriter that takes OpenOptions.
    The one you want to pass in is a the StandardOpenOption.APPEND.

    However, I would not recommend only appending the line after you've finished looking through the file for the given user.
    At the moment you seem to be doing it while you are still looking through the file. compare Strings (and all objects) using .equals(), and not ==.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Problem with BufferedWriter
    By salmontres in forum New To Java
    Replies: 5
    Last Post: 12-09-2011, 07:06 PM
  2. BufferedWriter Problem
    By dailywalker in forum New To Java
    Replies: 3
    Last Post: 04-04-2011, 10:07 PM
  3. BufferedWriter, FileWriter
    By ladykrimson in forum New To Java
    Replies: 4
    Last Post: 12-06-2010, 02:35 AM
  4. BufferedWriter
    By Karthika in forum New To Java
    Replies: 6
    Last Post: 04-22-2010, 09:23 AM
  5. Replies: 0
    Last Post: 06-26-2008, 07:33 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts