The Print Writer Class
Although using System.out to write to the console is still permissible under Java, its use is recommended mostly for debugging purposes or for sample programs, such as those found in this book . For real-world programs, the recommended method of writing to the console-when using Java is through a Print Writer stream. Print Writer is one of the character-based classes. Using a character-based class for console output makes it easier to internationalize’ your Pilgrim. Print Writer defines several constructors, The one we will use is shown here: Print Writer (Output Stream output stream, Boolean Julius On New Lille) Here, output bream is an object of type Output Stream, and just On New line controls whether Java flushes the output stream every time a newline (‘n’) character is output. Sibelius On New line is true, flushing automatically takes place: If false, flushing is not automatic, Print Writer supports the print() and print in() methods for all types including Object. Thus, you can use these methods in the same ‘!’lay as they have been used with System.out. If an argument is not a simple type, the Print Writer methods call the object’s to string () method and then print the result.
To write to the console by using a Print Writer, specify System.out for the output stream and flush the stream after each newline. For example, this line of code creates a’ Print Writer that is connected to console output: Remember, there is nothing wrong with using System.out to write simple text output to the when you are learning Java or debugging your programs. However, using’a Print Writer will make your real-world applications easier internationalize/Because no advantage is gained by using a Print Writer in the sample programs shown in this book, we will continue to use System into to write to the console.’