Advertisement
| 1) PrintWriter(File file) |
|---|
|
This constructor creates a PrintWriter object to write data to a file accessed by the File reference, file. Example-:
|
| 2) PrintWriter(OutputStream os) |
|---|
|
This constructor creates a PrintWriter object to write data to the output stream, os.
Example-:
|
| 3) PrintWriter(Writer w) |
|---|
|
This constructor takes an Writer object to write out data to write character stream, w.
Example-:
|
| 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
//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);
}
}
}
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement