Java - Console writer() method



Description

The Java 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

Important Note

System.console() may return null in IDEs like Eclipse, NetBeans or IntelliJ. Run these programs from the command line (terminal) for proper execution.

Example - Usage of Console writer() method

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

ConsoleDemo.java

package com.tutorialspoint;

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

public class ConsoleDemo {
   public static void main(String[] args) {      
      Console console = null;
      PrintWriter out = null;

      try {
         // creates a console object
         console = System.console();

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

Console Interaction

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

Here is The Optimus Prime!!

Example - Using writer() to Print a Simple Message

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

ConsoleDemo.java

package com.tutorialspoint;

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

public class ConsoleDemo {
   public static void main(String[] args) {
      Console console = System.console();
      if (console == null) {
         System.out.println("No console available");
         return;
      }

      // Get the PrintWriter from the Console
      PrintWriter writer = console.writer();

      // Write messages using PrintWriter
      writer.println("Hello, this is a message from Console.writer()!");
      writer.println("This method allows writing formatted output.");
      writer.flush();
   }
}

Console Interaction

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

Hello, this is a message from Console.writer()!
This method allows writing formatted output.

Explanation

  • We obtain the Console instance using System.console().

  • We check if the console is available (it doesn't work in most IDEs).

  • We call console.writer() to get a PrintWriter instance.

  • We use writer.println() to print messages to the console.

Example - Writing Formatted Output Using writer()

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

ConsoleDemo.java

package com.tutorialspoint;

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

public class ConsoleDemo {
   public static void main(String[] args) {
      Console console = System.console();
      if (console == null) {
         System.out.println("No console available");
         return;
      }

      // Get PrintWriter from Console
      PrintWriter writer = console.writer();

      // Get user input
      String name = console.readLine("Enter your name: ");
      int age = Integer.parseInt(console.readLine("Enter your age: "));

      // Write formatted output using writer
      writer.printf("Hello, %s! You are %d years old.\n", name, age);
      writer.flush();
   }
}

Console Interaction

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

Enter your name: Mahesh
Enter your age: 40
Hello, Mahesh! You are 40 years old.

Explanation

  • We obtain a PrintWriter from the Console instance.

  • We read the user's name and age using readLine().

  • We use writer.printf() to print a formatted message with the user's input.

java_io_console.htm
Advertisements