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.

I am appending the file as follows:

```
val fw = new FileWriter("src/file.txt", true) ;
fw.write("new item");

```

Can anybody please help me?

EDIT:

```
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()

```

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.