• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

formatting a very large string for a PrintWriter?

 
Ranch Hand
Posts: 35
1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I'm still in control here. LOOK at this tiny ad!
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic