You are writing to the same file you are reading from ("new.txt", as per your example). PrintWriter
truncates existing files to 0 bytes on opening. Therefore, as soon as you open it, you erase the data in it, and your Scanner
has nothing to read.
A traditional approach, when the input and output file are the same, would be to write the output to a new temporary file, and then replace the original file with the temporary file once all processing is complete.
An alternate approach, although far less convenient in your situation, would be to load the input file entirely into memory before opening the output, process the data, then write the processed data out.