So I've recently received a large text file that I need to read unfortunately its formatted terribly as there are next line characters everywhere making it extremely difficult to read. So I've been trying to think of a way to sort through the file and delete each nextLine
using a java program.
For example if we had this text:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse rhoncus interdum condimentum. Proin viverra
justo vel imperdiet sagittis, purus sapien sagittis mi, et blandit purus ante non libero. Nulla ac
augue ut odio eleifend interdum
ac id justo. Quisque rutrum euismod sem, vel euismod nunc convallis eu. Praesent odio velit,
condimentum id scelerisque
iaculis, vulputate vitae lacus. Fusce adipiscing blandit libero eu venenatis.
Vestibulum nec urna
pulvinar arcu pretium
pretium. Duis vitae augue dolor. Etiam consectetur feugiat
diam sit amet gravida.
we would like it to turn out like this :
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse rhoncus interdum
condimentum. Proin viverra, justo vel imperdiet sagittis, purus sapien sagittis mi,
blandit purus ante non libero. Nulla ac augue ut odio eleifend interdum ac id justo.
Quisque rutrum euismod sem, vel euismod nunc convallis eu. Praesent odio velit,
condimentum id scelerisque iaculis, vulputate vitae lacus. Fusce adipiscing blandit
libero eu venenatis. Vestibulum nec urna pulvinar arcu pretium pretium. Duis vitae
augue dolor
I'm not very experienced with reading/writing text file using Java. My current idea is reading in each char value checking if it is equal to the value of next line and proceeding to write that char to a separate text file if it is not. But i'm not really sure on how to implement this. I'm not sure how to check if a the next character is a next line.
Any help would be great.
You can use a Scanner to do this.
I suggest that you use a BufferedReader, and make use of the
readLine()
method to read the file a line at a time.When you read an empty line, the method will return a (non-null) empty string.
When you get to the end of file, the method will return a null.
Otherwise, the method will return the line ... with the line terminator removed. (So when you output the line, don't forget to add a line terminator!)
This should be enough for you to write the program yourself. (And that would be a GOOD THING!)
You could also just write back to the same file, but that would require using a
StringBuilder
.