import java.io.Console; public class ConsoleInputExample { public static void main(String[] args) { Console console = System.console(); if (console == null) { System.out.println("Console not available."); return; } String name = console.readLine("Enter your name: "); System.out.println("Hello, " + name); } }
import java.io.Console; public class ConsoleOutputExample { public static void main(String[] args) { Console console = System.console(); if (console == null) { System.out.println("Console not available."); return; } console.writer().println("Hello, World!"); } }In this example, we use the System console to write output. We use the writer() method to obtain a PrintWriter object and then use println() to write "Hello, World!" to the console. These examples are part of the java.io package library.