Java Console.writer()

Syntax

Console.writer() has the following syntax.

public PrintWriter writer()

Example

In the following code shows how to use Console.writer() method.


import java.io.Console;
import java.io.PrintWriter;
/*  w  w  w.j av  a 2 s  .c o  m*/
public class Main {
  public static void main(String[] args) throws Exception {

    // creates a console object
    Console cnsl = System.console();

    // if console is not null
    if (cnsl != null) {

      // creates new print writer
      PrintWriter out = cnsl.writer();

      // prints
      out.println("Here is The Optimus Prime!!");
    }

  }
}

The code above generates the following result.