PrintStream Class In Java
PrintStream Class
In Java, PrintStream class provides the methods to the write data to another stream.
In other words, Java.io.PrintStream class adds functionality to another output stream and the ability to print representations of the various data values easily.
This class automatically flushes the data. Thus, there is no need to call flush() method. Besides, its methods does not throw an IOException.
Methods of PrintStream class
public void print(boolean b)
This method is used to print the specified Boolean value.
public void print(char c)
This method is used to print the specified char value.
public void print(char[] c)
This method is used to print the specified character array values.
public void print(int i)
This method is used to print the specified int value.
public void print(long l)
This method is used to print the specified long value.
public void print(float f)
This method is used to print the specified float value.
public void print(double d)
This method is used to print the specified double value.
public void print(String s)
This method is used to print the specified string value.
public void print(Object obj)
This method is used to print the specified object value.
public void println(boolean b)
This method is used to print the specified Boolean value and terminate the line.
public void println(char c)
This method is used to print the specified char value and terminate the line.
public void println(char[] c)
This method is used to print the specified character array values and terminate the line.
public void println(int i)
This method is used to print the specified int value and terminate the line.
public void println(long l)
This method is used to print the specified long value and terminate the line.
public void println(float f)
This method is used to print the specified float value and terminates the line.
public void println(double d)
This method is used to print the specified double value and terminate the line.
public void println(String s)
This method is used to print the specified string value and terminate the line.
public void println(Object obj)
This method is used to prints the specified object value and terminate the line.
public void println()
This method is used to terminate the line only.
public void printf(Object format, Object... args)
This method is used to write the formatted string to the current stream.
public void printf(Locale l, Object format, Object... args)
This method is used to write the formatted string to the current stream.
public void format(Object format, Object... args)
This method is used to write the formatted string to the current stream using specified format.
public void format(Locale l, Object format, Object... args)
This method is used to write the formatted string to the current stream using specified format.
Let’s see an example, given below.
Code
- import java.io.*;
- public class PrintStreamClass {
- public static void main(String args[]) throws Exception {
- FileOutputStream fos = new FileOutputStream("Java.txt");
- PrintStream ps = new PrintStream(fos);
- ps.println(1500);
- ps.println("Hello, I am Java");
- ps.close();
- fos.close();
- }
- }
In the example, mentioned above, we can see that PrintStream class easily prints both integer and string values.
Summary
Thus, we learnt that printStream class adds functionality to another output stream and the ability to print the representations of the various data values easily and also learnt its important methods in Java.