title: How to delete the last line of the file in scala?
body: <p>I am trying to append to a file such that I first want to delete the last line and then start appending. But, I can't figure how to delete the last line of the file.</p>
  
  <p>I am appending the file as follows:</p>
  
  <pre><code>val fw = new FileWriter("src/file.txt", true) ;
  fw.write("new item");
  </code></pre>
  
  <p>Can anybody please help me?</p>
  
  <p>EDIT:</p>
  
  <pre><code>val lines_list = Source.fromFile("src/file.txt").getLines().toList 
  val new_lines = lines_list.dropRight(1) 
  val pw = new PrintWriter(new File("src/file.txt" )) 
  (t).foreach(pw.write) pw.write("\n") 
  pw.close()
  </code></pre>
  
  <p>After following your method, I am trying to write back to the file, but when I do this, all the contents, with the last line deleted come in a single line, however I want them to come in separate lines.</p>