Home » Java programming language

Java Console writer() Method with Example

Console Class writer() method: Here, we are going to learn about the writer() method of Console Class with its syntax and example.
Submitted by Preeti Jain, on March 31, 2020

Console Class writer() method

  • writer() method is available in java.io package.
  • writer() method is used to get a distinct PrintWriter object linked with this Console.
  • writer() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • writer() method does not throw an exception at the time of returning associated PrintWriter.

Syntax:

    public PrintWriter writer();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of the method is PrintWriter, it gets PrintWriter object linked with this Console.

Example:

// Java program to demonstrate the example 
// of PrintWriter writer() method of Console

import java.io.*;

public class WriterOfConsole {
 public static void main(String[] args) {
  try {
   // Instantiates Console , PrintWriter
   // object
   Console con = System.console();
   PrintWriter pw = con.writer();

   // Display by using PrintWriter 
   // object pw
   pw.println("con.writer(): " + "Java Programming!!!");

  } catch (Exception ex) {
   System.out.println(ex.toString());
  }
 }
}

Output

con.writer(): Java Programming!!!


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.