This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Java in General and the fly likes Printing out a lot of Text to the Screen without Using a lot of <println()>s Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Printing out a lot of Text to the Screen without Using a lot of <println()>s" Watch "Printing out a lot of Text to the Screen without Using a lot of <println()>s" New topic
Author

Printing out a lot of Text to the Screen without Using a lot of <println()>s

Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 103
Currently I have a Java program that a co-worker requested I write. It turned out to be pretty complex, and I decided that if a user tried to execute my tool with "java CpyTree -help" it would print all the documentation that explains how to use it. That's a whole page of text.

As my program is now written, I just call <System.out.println()> over and over again to print all the text. But I seem to recall from a long time ago that there's a way to tell the JVM to simply print the contents of a Java source file from a starting point until it reaches a special string.

I did a Google search on "java writing a lot of text", but that didn't give me anything useful. Does anyone know the exact syntax for printing out a lot of text to the screen without using fifty-plus <System.out.println()> calls? Or can you point me to somewhere in the documentation where it explains how to do this? Thanks in advance for any help you can give me on this.

Kevin Simonson
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

So you want to store your help text in a file or a resource, instead of hard-coding it in your program? (Good plan by the way.) And then you want to copy that file or resource to the console, right? If you google "java file copy" you should find some examples.
Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 103
Paul Clapham wrote:So you want to store your help text in a file or a resource, instead of hard-coding it in your program? (Good plan by the way.) And then you want to copy that file or resource to the console, right? If you google "java file copy" you should find some examples.

Well, no, I wanted to hard code the help text in my Java source file; I just wanted to do it without putting in half a million <println()>s. Instead of typing in:

I wanted to just type in:

Obviously there's more to my help text than these three lines, but you get the point.

I certainly could read my help text from a file. Would that be better than hard coding it into my Java source file?

Kevin Simonson
Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 122
I was kind of thinking along the same lines. Depending on how the class is being deployed, a simple process of reading a file and dumping it to the console would work. The sticky point to me just thinking about it quickly is that you'd have to have to deploy it in a manner that you know where the file is in order to read it. A simple text file would seem to be the easiest to work with in that you'd just key in whatever you literally want echo'd to the console spacing, line breaks etc. It would be readily editable.

I haven't worked with resource bundles to store paragaphs of information. Not sure how that would work as you may end up having the text in a format that isn't as editable or "readble" in code context.

I'll throw another idea out there that shouldn't be too hard to build if either of the above options do not work. One of the simpler approaches to get the data would be to just have the lines of text in an array and iterate through the array printing each line of text. To make the data the most editable and avoid lots of handfitting the information into the array, what about setting up the help information in a text file and create a helper class which reads that file and generates a class containing the array declation and sets the various lines from the file as elements in the array? The class with which you want to display "-help" from just needs to loop through the array and dump it to System.out.println.

Depending on how often you create such classes, accepting some inputs to the generation utility for class name to generate, package to generate, etc could make it a fairly useful tool. The generated class could also be built with a method to dump the array contents to System.out or better yet with a method that accepts a PrintStream and dumps to whatever stream it is provided.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

If you only have half-a-dozen lines of help text then it isn't worth storing it in an external resource. On the other hand if you have half a million (your facetious estimate!) lines then you definitely should.

Perhaps your problem boils down to you thinking you have to output one line at a time? The escape sequence "\n" represents a new-line character and you can use that inside String literals.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38045
    
  22
Better to use the %n sequence and the printf method or similar. You can use %n for the String.format() method too.
Kevin Simonson
Ranch Hand

Joined: Oct 22, 2011
Posts: 103
Campbell Ritchie wrote:Better to use the %n sequence and the printf method or similar. You can use %n for the String.format() method too.

I'm not sure I believe this. Is there really nobody out there who knows the exact syntax for taking Java code:

and transforming it into:

I have a distinct memory of being told how to do this, but I can't remember the exact syntax for doing it. Am I remembering wrong? Is it C syntax maybe, instead of Java, or maybe C++?

Kevin Simonson
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Paul Clapham wrote:Perhaps your problem boils down to you thinking you have to output one line at a time? The escape sequence "\n" represents a new-line character and you can use that inside String literals.

Brian Burress
Ranch Hand

Joined: Jun 30, 2003
Posts: 122
Kevin,

I won't swear that what you are describing does not exist, but I can say I have been working with Java for a little over 10 years and have never run across something like what you are describing.

Assuming what you are recalling does not exist, Paul's example is probably the simplest solution for you and is pretty close to what you are looking for with the exception that you need to wrap your text with quotes and a +.

If you are going to work with a lot of data in the help or something that changes a lot, some of the other suggestions may be better to look into further.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Printing out a lot of Text to the Screen without Using a lot of <println()>s
 
Similar Threads
java printing - printing a pdf
Print a document using java API
Printing Text
checking for only part equivalence between Objects
Documents