aspose file tools*
The moose likes Java in General and the fly likes formatting a very large string for a PrintWriter? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "formatting a very large string for a PrintWriter?" Watch "formatting a very large string for a PrintWriter?" New topic
Author

formatting a very large string for a PrintWriter?

Rob MacKay
Ranch Hand

Joined: Apr 06, 2007
Posts: 35
    
    1
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
Bartender

Joined: Oct 14, 2005
Posts: 18991
    
    8

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

Joined: Apr 06, 2007
Posts: 35
    
    1
Correct. Basically looking for a word wrapping utility that can take a parameter for the number of characters per line and then adjust accordingly.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: formatting a very large string for a PrintWriter?