
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
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.