Curriculum
In Java, PrintStream
is a class that provides a convenient way to print data to an output stream, such as a file or the console. It is a subclass of FilterOutputStream
and adds functionality to convert primitive data types and objects to their string representation before writing them to the output stream. Here is an example of how the PrintStream
class is used in Java:
import java.io.FileOutputStream; import java.io.IOException; import java.io.PrintStream; public class PrintStreamExample { public static void main(String[] args) { try { FileOutputStream fileOutput = new FileOutputStream("example.txt"); PrintStream printOutput = new PrintStream(fileOutput); printOutput.println("Hello, World!"); printOutput.printf("The value of pi is approximately %.2f%n", Math.PI); printOutput.close(); } catch (IOException e) { System.out.println("An error occurred while writing to the file: " + e.getMessage()); } } }
In this example, we create a FileOutputStream
to a file named “example.txt”, and we use it to create a PrintStream
. We use the println()
method to write the string “Hello, World!” to the output stream, and we use the printf()
method to write the value of pi to the output stream with two decimal places. We then close the PrintStream
using the close()
method.
The PrintStream
class in Java has the following methods:
void close()
: Closes the output stream and releases any system resources associated with it.void flush()
: Flushes the output stream, forcing any buffered output bytes to be written to the underlying output stream.void print(boolean b)
: Prints a boolean value to the output stream.void print(char c)
: Prints a character to the output stream.void print(double d)
: Prints a double value to the output stream.void print(float f)
: Prints a float value to the output stream.void print(int i)
: Prints an integer value to the output stream.void print(long l)
: Prints a long value to the output stream.void print(Object obj)
: Prints an object to the output stream.void print(String s)
: Prints a string to the output stream.void println()
: Prints a newline character to the output stream.void println(boolean x)
: Prints a boolean value to the output stream, followed by a newline character.void println(char x)
: Prints a character to the output stream, followed by a newline character.void println(double x)
: Prints a double value to the output stream, followed by a newline character.void println(float x)
: Prints a float value to the output stream, followed by a newline character.void println(int x)
: Prints an integer value to the output stream, followed by a newline character.void println(long x)
: Prints a long value to the output stream, followed by a newline character.void println(Object x)
: Prints an object to the output stream, followed by a newline character.void println(String x)
: Prints a string to the output stream, followed by a newline character.Note that when using a PrintStream
, it is important to properly close the output stream using the close()
method to ensure that all system resources are released. Additionally, the PrintStream
is a convenience class that is primarily intended for printing text-based data to an output stream. For more complex output formatting, it