Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

formatting a very large string for a PrintWriter?

 
Rob MacKay
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Paul Clapham
Sheriff
Posts: 20713
29
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob MacKay
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. Basically looking for a word wrapping utility that can take a parameter for the number of characters per line and then adjust accordingly.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic