Java PrintStream ClassLast Updated : 28 Jan 2026 The PrintStream class in Java is used to write formatted and unformatted data to another output stream. In this chapter, we will learn what the PrintStream class is, why it is used, its declaration, constructors, important methods, and how to print data to files and the console using examples. What is PrintStream Class in Java?The PrintStream class is an output stream that provides convenient methods to print data values such as primitives, strings, and objects in a readable form. It automatically flushes the output, so there is no need to explicitly call the flush() method. Also, its printing methods do not throw IOException, which makes it easier to use. The System.out object is a well-known example of a PrintStream. PrintStream Class DeclarationThe following declaration shows how the PrintStream class is defined: Constructors of PrintStream ClassThe PrintStream class provides constructors to create a print stream connected to another output stream. 1. PrintStream(OutputStream out)This constructor creates a new print stream using the specified output stream. Syntax: Here is the syntax: 2. PrintStream(String fileName)This constructor creates a new print stream using the specified file name. Syntax: Here is the syntax: Example of PrintStream Class ConstructorsThe following code shows how the constructors of the PrintStream class are used. Methods of PrintStream ClassThe PrintStream class provides methods to print data in different formats.
Examples of Java PrintStream ClassThe following examples demonstrate how to use the Java PrintStream class. Example 1: Writing Data to a File Using PrintStreamThe following example demonstrates how to write integer and string data to a file using the PrintStream class. Output: Success... File Content (testout.txt): 2016 Hello Java Welcome to Java Example 2: Using printf() Method with PrintStreamThe following example demonstrates how to print formatted output using the printf() method of the PrintStream class. Output: 19 Next TopicJava PrintWriter class |
We request you to subscribe our newsletter for upcoming updates.