Advertisement



< Prev
Next >



PrintWriter



With the most of Writer classes we can write data only in terms of characters but with PrintStream Writer class we can write data not only in terms of characters but also in terms of primitive data types like int, float, long, double and even an Object. PrintStream class can be wrapped around any OutputStream or a Writer class, to write data to a file.




Constructors :


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


2) PrintWriter(OutputStream os)
This constructor creates a PrintWriter object to write data to the output stream, os.
Example-:
FileOutputStream fos=new FileOutputStream("D:\\TextBook.txt");
PrintWriter pw =new PrintWriter(fos);
A PrintWriter object is created to write data to a file D:\\TextBook.txt, accessed by FileOutputStream reference, fos.


3) PrintWriter(Writer w)
This constructor takes an Writer object to write out data to write character stream, w.
Example-:
FileWriter fw=new FileWriter("D://TextBook.txt");
PrintWriter pw =new PrintWriter(fos);
A PrintWriter object is created to write data to a file D:\\TextBook.txt, pointed by FileWriter reference, fw.




Methods for writing PrintWriter


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

Methods Description
flush() This method flushes any buffered data to be written out of this character output stream.
write() This method writes one byte at a time to this character output stream.
write(byte[]) This method writes a whole byte array at a time to this character output stream.
print(char c) This method prints a character(primitive data type) to this character 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 character 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 to this character output stream.
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, pointed by output stream, FileOutputStream.
//A program to create a output Stream for writing characters and primitive data types.

import java.io.*;
class A
{

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

public static void main(String... ar)
{
try
{
A ob = new A();
File file= new File("D:\\TextBook.txt"); 

PrintWriter pw = new PrintWriter(file);

pw.println(1985);
pw.print('C');
pw.println(23.45f);
pw.println("Java Rules");
pw.println(ob)
pw.flush();
pw.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 Rules
An object is created


Program Analysis





Please share this article -




< Prev
Next >
< BufferedWriter
Console >



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