Introduction to Java PrintStream
Java PrintStream has the capacity to print the depiction of many data values and adds functionality to a different output stream. The specialty of a print stream is that it does not throw an IOException like other input streams, and they set a flag internally to indicate an exception occurs that can be tested using the checkError method (This happens only during exceptional cases). It can also be created such that to flush itself automatically.
The characters are converted to bytes that PrintStream generates with the platform’s in-built character encoding. Hence, this PrintWriter class is used to be in places that require writing characters instead of bytes for int, long, etc.
Syntax:
public class PrintStream
extends FilterOutputStream
implements Appendable, Closeable
PrintStream, as shown, is inherited from the class FilterOutputStream, and the interfaces implemented from the same are Appendable and Closeable.
Constructors of Java PrintStream
Below are the constructors and descriptions used by the PrintStream function:
- PrintStream(File file, String csn): This creates a new print-stream with the given file and charset without any automatic flushing.
- PrintStream(File file): This also creates a new print-stream with the specified file. This also does not have automatic flushing.
- PrintStream(OutputStream out, boolean autoFlush): This also creates a new print stream.
- PrintStream(OutputStream out): Accepts only a single parameter and creates a new print stream.
- PrintStream(String fileName): Accepts the filename as input parameter and creates a new print-stream without having provision for automatic line flushing.
- PrintStream(OutputStream out, boolean autoFlush, String encoding):
This accepts 3 input parameters as shown and creates a new print stream. - PrintStream(String fileName, String chs): This also creates a new print-stream that is not having automatic line flushing with the given file name and charset.
List of Methods Used
1. PrintStream append(char a): This method is used to append the given character to the output stream
Syntax:
public PrintStream append(char a)
Parameters required: It takes the input parameter as the character type a – Appends to the 16-bit character.
Returns: The output stream
2. PrintStream appfin(CharSequence chs, int st, int fin): This function requires 3 parameters and also appends the given character sequence to this output stream.
Syntax:
public PrintStream appfin(CharSequence chs,
int st,
int fin)
Parameters Required:
- chs: Input character sequence is taken from this, and a subsequence will be appended here.
- st: This represents the first character’s index in the subsequence.
- fin: This represents the last character’s index which follows in the subsequence.
- Returns: The output stream.
- Throws: exception like IndexOutOfBoundsException.
3. PrintStream append(CharSequence chs): This method is used to append a subsequence of the given character sequence to this output stream.
Syntax:
public PrintStream append(CharSequence chs)
Parameters required:
- chs: The character sequence which has to be appended.
- Returns: The output stream.
4. Boolean checkError(): This is used to flush the stream and get its error state status.
Syntax:
public boolean checkError()
Return parameters: Returns boolean true value only if this stream has encountered an IOException
and returns false if any other exception like InterruptedIOException, or if the setError method has been called.
5. protected void clearError(): This method is used to clear any of the stream’s internal error states.
Syntax :
6. protected void clearError()
7. void flush(): Another function with no return parameters and is used to flush the stream.
Syntax :
8. public void flush(): This method overrides the flush function of class FilterOutputStream
9. void close(): The basic method used for closing the stream.
Syntax:
public void close()
This method overrides the close() function of class FilterOutputStream
10. PrintStream format(Locale loc, String fr, Object… arg): This function is used to write a string that is formatted to the output stream using the given format string and parameters.
Syntax:
public PrintStream format(Locale loc,
String fr,
Object... arg)
Parameters required:
- loc: The locale we use during formatting. If this value is null, then no need to apply localization.
- arg: All the formal specifiers use these arguments as a reference in the format string. The arguments passed here can range from zero to many.
- Return parameters: The output stream. Throws 2 kinds of exception IllegalFormatException and NullPointerException
11. PrintStream format(String for, Object… args): Used to write a formatted string to the output stream using the given format string and parameters.
Syntax:
public PrintStream format(String for,
Object... args)
Parameters required:
- for: Format string as per the syntax
- args: Input arguments as described, and they may range from zero to many
- Return parameters: The output stream. Throws 2 kinds of exception IllegalFormatException and NullPointerException
Example to Implement Java PrintStream
Below is an example of Java PrintStream. First, let us undertake a basic example to understand the above discussed different methods of PrintStream.
Code:
import java.io.*;
import java.util.Locale;
//Java code to display different methods of Printstream
public class Main
{
public static void main(String args[]) throws FileNotFoundException
{
// Creating an output file to write the output
FileOutputStream file=new FileOutputStream("output.txt");
// Creating object of PrintStream
PrintStream op=new PrintStream(file);
String str="Example";
// Writing below to output.txt
char a[]={'F','I','R','S','T'};
// Example for print(boolean b) method
op.print(true);
op.println();
// Example for print(int a) method
op.print(1);
op.println();
// Example for print(float f) method
op.print(5.10f);
op.println();
// Example for print(String str) method
op.print("Example code for PrintStream methods");
op.println();
// Example for print(Object ob) method
op.print(file);
op.println();
// Example for append(CharSequence chs) method
op.append("Append method");
op.println();
//Example for checkError() method
op.println(op.checkError());
//Example for format() method
op.format(Locale.US, "This is a %s program", str);
//Example for flush method
op.flush();
//Example for close method
op.close();
}
}
Output:
Explanation: This example generates an output file, and we are displaying all the method related outputs by writing them into the output.txt file. This creates a file if it does not exist, and hence the output will not be visible in the IDE. We are first creating a PrintStream object here and then using that to showcase all the functioning of methods like print(boolean b), print(int I), print(float f), print(String s) and other methods as shown in the code.
Conclusion
Hence as discussed above, a PrintStream in java that is basically used to write formatted data to the output stream. The naming is done as per its functionality that it formats the primitive values like int, long into text like as to when they will look when displayed on a screen.
Recommended Articles
This is a guide to Java PrintStream. Here we discuss the Introduction to Java PrintStream and its different Constructors along with an example. You can also go through our other suggested articles to learn more –