This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I have a use case where I need to read a text file read a text file (possibly quite a large one) into an InputStream and then display the contents to the System.out.
Simple enough, I am using the 1.6 Console class to access it's, which is pretty much -> PrintWriter.println(mytext). This works fine, except that the entire contents are just dumped to the screen, which is pretty silly for a very large text file since there would be no way to read it all.
What I want to be able to do is limit the amount of the text file getting dumped to the screen so that the user can read it and effectively "click" through it by hitting enter or any other key. Basically, paging through the text until it's all written to the screen.
Are they any handy algorithms around that can format the large String into something that PrintWriter can handle in this way?
I have a few ideas but don't want to re-invent the wheel.
The wheel you're trying to reinvent is where the user types this at the command line:
java YourProgram | more
This pipes the output of YourProgram to the "more" command, which simply pauses after each screenful of output and says "Press any key to continue" or something like that.
You could do the same thing in your code; write N lines and then ask the user to hit the Enter key to continue. Which would be, as you say, re-inventing the wheel. But there's no way to insert magic characters into the output and have the console pause, unless you then write something which can read that output from the pipe and interpret those magic characters.
Joined: Apr 06, 2007
Correct. Basically looking for a word wrapping utility that can take a parameter for the number of characters per line and then adjust accordingly.