Advertisement



< Prev
Next >



PrintStream Class



With the most of OutputStream classes we can write data only in terms of bytes but with PrintStream we can write any primitive data type or even an Object. PrintStream class is a subclass of FilterOutputStream and it can be wrapped around any output stream class to filter the data to the output stream.

Note :- Here filtration doesn't mean removing the impurities as it's in the case a water filter but filtering the data means modifying the data in a specific way, so that it could be sent to the output stream in terms of any primitive data type and Objects.




Constructors :


1) PrintStream(File file)
This constructor creates a PrintStream object to write data to a file specified by the File object.
Example-:
File file = new File("D:\\TextBook.txt");
PrintStream ps =new PrintStream(file);
We have created a PrintStream object to write data out to a file "D:\\TextBook.txt", pointed by a File object. If the file exists, it will be truncated to zero size, otherwise, a new file will be created.

2) PrintStream(OutputStream os)
This constructor takes an OutputStream object in the parameters to write data to this output stream.
Example-:
FileOutputStream fos=new FileOutputStream("D:\\TextBook.txt");
PrintStream dis =new PrintStream(fos);
We have created PrintStream object wrapped around FileOutputStream to write filtered data to a file D:\\TextBook.txt




Methods for writing PrintStream


Some methods that we generally use while working with PrintStream are shown in the table below :

Methods Description
flush() This method flushes any buffered data to be written out of this output stream.
write() This method writes one byte at a time to this output stream.
write(byte[]) This method writes a whole byte array at a time to this output stream.
print(char c) This method prints a character(primitive data type) to this output stream. print() methods don't add a newline character.
print(int i) This method prints an int(primitive data type).
print(long l) This method prints a long(primitive data type).
print(float f) This method writes a float (primitive data type)
print(double d) This method writes a double (primitive data type).
print(boolean b) This method writes a boolean(primitive data type).
println(char c) This method prints a character(primitive data type) to this output stream. println() methods add a newline character.
println(int i) This method prints an int(primitive data type).
println(long l) This method prints a long(primitive data type).
println(float f) This method writes a float (primitive data type).
println(double d) This method writes a double (primitive data type).
println(boolean b) This method writes a boolean(primitive data type).
println(Object o) This method writes an Object.
close() This method closes this output stream and also frees any resources connected with this output stream.



Advertisement




Writing primitive data types and an object to a file using PrintStream.


In this program, we have created a PrintStream object to write primitive data types to a file D:\\TextBook.txt, using the output stream FileOutputStream.
import java.io.*;
class A
{

public String toString()
{
return "-An object is created-" ;
}

public static void main(String... ar)
{
A ob= new A();

try
{
FileOutputStream fos= new FileOutputStream("D://TextBook.txt");
PrintStream ps= new PrintStream(fos);

ps.println(1985);
ps.print('C');
ps.println(23.45f);
ps.println("Java Rules");
ps.println(ob);

ps.flush();
ps.close();
fos.close();
}
catch(Exception e)
{
System.out.println(e);
}

}
}



Output is -:


After the execution of this program, a file "TextBook.txt" is created in the D: drive, with contents -:
1985
C23.45
Java
-An object is created


Program Analysis






Please share this article -




< Prev
Next >
< DataOutputStream
CharacterStream>



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement