Well, as PrintWriter
is just a Writer
, which can only append
/write
/print
So, you can't override the line which you just have written.
Several options you have :
- Modify your logic to make sure you don't write the line you want to remove eventually (I think the most logical option)
- After writing to file you can use another
Reader
(say,BufferedReader
) to read it again, and then re-write it, without the line you'd like to exclude. - use RandomAccessFile and its
seek
method to go back and rewrite / remove the line you need.