Java Print Writer
PrintWriter:
It is used to write output data from the file. It is the class of Java.io package. It inherits the properties of the Writer class.
Writer Class:
It is a class of Java.io package, it represents the set of characters. It is an abstract class.
Writer Subclasses:
- Buffered Writer
- File Writer
- String Writer
- Output Stream Writer
- PrintWriter
Writer class object creation Syntax:
Writer object1 = new FileWriter();
Note:
Here, to create an object we use FileWriter because the Writer class is abstract.
Working Of PrintWriter Class: It is used to convert all primitive data types such as int, long, and double to text format. It does not throw any Input or Output exceptions to find errors we use the checkError() method.
Constructors in PrintWriter class:
- PrintWriter (File f)
- PrintWriter (File f, String str1)
- PrintWriter (OutputStream output)
- PrintWriter (OutputStream output, boolean autoFlush)
- PrintWriter (String fN)
- PrintWriter (String fN, String str1)
- PrintWriter (Writer output)
- PrintWriter (Writer output, boolean autoFlush)
1.PrintWriter (File f):It creates or generates a print writer object with the particular file without automatic line flush.
Syntax for Object Creation:
PrintWriter obj1 = new PrintWriter(File f);
2.PrintWriter (File f, String str1):It creates or generates a print writer object with the particular file and character set, without an automatic flush line.
Syntax for Object Creation:
PrintWriter obj1 = new PrintWriter(File f, String str1);
3.PrintWriter (OutputStream output):It creates or generates a print writer object with an existing output stream and without an automatic flush line.
Syntax for Object Creation:
PrintWriter obj1 = new PrintWriter (OutputStream output);
4.PrintWriter (OutputStream output, boolean autoFlush):It creates or generates a print writer object with an existing output stream and with an existing automatic flush line.
Syntax for Object Creation:
PrintWriter obj1 = new PrintWriter (OutputStream output, boolean autoFlush);
5.PrintWriter (String fN):It creates or generates a print writer object with the particular file name and without existing any automatic flush line.
Syntax for Object Creation:
PrintWriter obj1 = new PrintWriter (String fN);
6.PrintWriter (String fN, String str1):It creates or generates a print writer object with the particular file name and character set, without existing any automatic flush line.
Syntax for Object Creation:
PrintWriter obj1 = new PrintWriter (String fN, String str1);
7.PrintWriter (Writer output):It creates or generates a print writer object, without any existing automatic flush line.
Syntax for Object Creation:
PrintWriter obj1 = new PrintWriter (Writer Output);
8.PrintWriter (Writer output, boolean autoFlush):It creates or generates a print writer object, with an existing automatic flush line.
Syntax for Object Creation:
PrintWriter obj1 = new PrintWriter (Writer Output, boolean autoFlush);
Methods in PrintWriter class:
- PrintWriter append (char c)
- PrintWriter append (CharSequence cs, int start, int end)
- PrintWriter append (CharSequence cs)
- checkError ()
- clearError ()
- close ()
- flush ()
- print (boolean b)
- print (char ch)
- print (char[] str)
- print (double d)
- print (float f)
- print (int i)
- print (long l)
- print (Object obj)
- print (String s)
PrintWriter append (char c):It appends the particular character to the writer. It appends the 16 bit characters.
Syntax For Method:
public PrintWriter append (char c)
PrintWriter append (CharSequence cs, int start, int end):It appends the specified character sequence to the file.
Syntax For Method:
public PrintWriter append (CharSequence cs, int start, int end)
cs : Character Sequence.
- Start : The index of the first character in character sequence.
- End: The index of the last character.
PrintWriter append (CharSequence cs): It appends a subsequence of specified character to the writer.
Syntax For Method:
public PrintWriter append (CharSequence cs)
checkError ():It checks the error state of the program.
Syntax For Method:
public boolean checkError()
clearError ():It clears the error of the stream.
Syntax For Method:
protected void clearError()
close ():It closes the stream and releases any resources associated with it.
Syntax For Method:
public void close()
flush ():It flushes the stream.
Syntax For Method:
public void flush()
print (boolean b): It prints the boolean value.
Syntax For Method:
public void print(boolean b)
print (char ch):It prints the character value.
Syntax For Method:
public void print(char ch)
print (double d):It prints the double value.
Syntax For Method:
public void print(double d)
print (float f):It prints the float value.
Syntax For Method:
public void print(float f)
print (int i):It prints the integer value.
Syntax For Method:
public void print (int i)
print (long l): It prints the long value.
Syntax For Method:
public void print (long l)
print (Object obj):It prints an object.
Syntax For Method:
public void print (Object obj)
print (String s): It prints the string.
Syntax For Method:
public void print (String s)
Program:
import java.io.*;
class Example
{
public static void main (String args[])
{
String str1 = “Javatpoint”;
PrintWriter Output = new PrintWriter(System.out);
Output.print(true);
Output.println();
Output.print(1);
Output.println();
Output.print(14.53f);
Output.println();
Output.print(“Javatpoint”);
Output.println();
Output.print(out);
Output.println();
Output.append("Geek");
Output.println();
Output.println(out.checkError())
Output.flush();
Output.close();
}
}
Output:
True
1
14.53f
Javatpoint
Geek
false