import java.io.*; public class WriteToFile { public static void main(String[] args) { try { BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt")); writer.write("Hello, world!"); writer.newLine(); // writes a new line separator writer.close(); } catch (IOException e) { e.printStackTrace(); } } }
import java.io.*; public class WriteToConsole { public static void main(String[] args) { PrintWriter writer = new PrintWriter(System.out); writer.println("Hello, world!"); writer.flush(); } }In this example, we are using the `PrintWriter` class from the `java.io` package to write the string "Hello, world!" to the console. The `println()` method is called to write the text followed by a line separator. The `flush()` method is called to ensure that any buffered data is flushed to the console. The Java IO `writeLine` method is a part of the standard Java library, which is included in the Java Development Kit (JDK). It belongs to the `java.io` package, which contains classes for performing input and output operations in Java.