Home >Java >javaTutorial >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.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
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.
Below are the constructors and descriptions used by the PrintStream function:
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:
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:
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:
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:
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.
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.
The above is the detailed content of Java PrintStream. For more information, please follow other related articles on the PHP Chinese website!