Java.io.Console.writer() Method



Description

The java.io.Console.writer() method retrieves the unique PrintWriter object associated with this console.

Declaration

Following is the declaration for java.io.Console.writer() method −

public PrintWriter writer()

Parameters

NA

Return Value

This method returns the printwriter associated with this console.

Exception

NA

Example

The following example shows the usage of java.io.Console.writer() method.

package com.tutorialspoint;

import java.io.Console;
import java.io.PrintWriter;

public class ConsoleDemo {
   public static void main(String[] args) {      
      Console cnsl = null;
      PrintWriter out = null;
      
      try {
         // creates a console object
         cnsl = System.console();

         // if console is not null
         if (cnsl != null) {
            
            // creates new print writer
            out = cnsl.writer();
            
            // prints
            out.println("Here is The Optimus Prime!!");
         }  
         
      } catch(Exception ex) {
         // if any error occurs
         ex.printStackTrace();      
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Here is The Optimus Prime!!
java_io_console.htm
Advertisements