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.
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.
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.
Joined: Oct 22, 2011
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?
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.
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.
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.
Joined: Jun 30, 2003
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.