title: How to check if reading the last line AND if at last line, how to add a new line at the end of file in JAVA?
body: <p>I am relatively new to programming, especially in Java, so bear that in mind when answering.  </p>
  
  <p>I'm programming a simple collectible card game deck building program, but file reading/writing proved to be problematic.  </p>
  
  <p>Here is the code for "addDeck" method that I'm trying to get working:  </p>
  
  <pre><code>/**
   * Adds a deckid and a deckname to decks.dat file.
   */
  public static void AddDeck() throws IOException {
      // Opens the decks.dat file.
      File file = new File("./files/decks.dat");
      BufferedReader read = null;
      BufferedWriter write = null;
  
      try {
          read = new BufferedReader(new FileReader(file));
          write = new BufferedWriter(new FileWriter(file));
          String line = read.readLine();
          String nextLine = read.readLine();
  
          String s = null; // What will be written to the end of the file as a new line. 
          String newDeck = "Deck ";
          int newInd = 00; // Counter index to indicate the new deckid number.
                           // If there are already existing deckids in the file,
                           // this will be the biggest existing deckid number + 1.
  
          // If the first line (i.e. the whole file) is initially empty,
          // the following line will be created: "01|Deck 01", where the
          // number before the '|' sign is deckid, and the rest is the deckname.
          if (line == null) {
              s = "01" + '|' + newDeck + "01";
              write.write(s);
          }
  
          // If the first line of the file isn't empty, the following happens:
          else {
  
              // A loop to find the last line and the biggest existing deckid of the file.
              while (line != null) {
  
                  // The following if clause should determine whether or not the next
                  // line is the last line of the file.
                  if ((nextLine = read.readLine()) == null) {
  
                      // Now the reader should be at the last line of the file.
                      for (int i = 0; Character.isDigit(line.charAt(i)); i++) {
  
                          // Checks the deckid number of the last line and stores it.
                          s += line.charAt(i);    
                      }
  
                      // The value of the last existing deckid +1 will be stored to newInd.
                      // Also, the divider sign '|' and the new deckname will be added.
                      // e.g. If the last existing deckid of decks.dat file is "12",
                      // the new line to be added would read "13|Deck 13".
  
                      newInd = (Integer.parseInt(s)) + 1;
                      s += '|' + newDeck + newInd;
                      write.newLine();
                      write.write(s);
                  }
  
                  else {
  
                      // If the current line isn't the last line of the file:
                      line = nextLine;
                      nextLine = read.readLine();
                  }
              }
          }       
  
      } finally {
              read.close();
              write.close();
      }
  }
  </code></pre>
  
  <p>The addDeck method should make the decks.dat file longer by one line each time when invoked. But no matter how many times I invoke this method, the
  decks.dat has only one line that reads "01|Deck 01".</p>
  
  <p>Also, I need to make a method removeDeck, which removes one whole line from the decks.dat file, and I'm even more at a loss there.</p>
  
  <p>I would be so very grateful for any help!</p>