title: How to add text at second last line in text file using java
body: <p>I have a text file in which there are multiple lines. I need to edit this text file dynamically. But I want to add dynamic text at second last line of the text file.</p>
  
  <pre><code>class FileAppending 
  {
      public static void main(String args[]) {
  
          try{
  
              OutputStreamWriter writer = new OutputStreamWriter(
                    new FileOutputStream("file path\\x.txt", true), "UTF-8");
              int lines = 0;
              while (br.readLine != 0){
                  lines++; // by this i'll get exact line number
              }
              BufferedWriter fbw = new BufferedWriter(writer);
              fbw.write("append txt...");
              fbw.newLine();
              fbw.close();
          }catch (Exception e) {
              System.out.println("Error: " + e.getMessage());
          }
      }
  }
  </code></pre>
  
  <p>Now i need some thing which will help me to add text at second last line since i have total number of lines</p>