×

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

Related Topics

Zig Zag star and Number Pattern in Java

We covered many Java pattern applications in the preceding part. We will write Java applications for zigzag star and number patterns in this part. Printing Zig Zag Number Pattern Steps Print one...

3 minutes read.

Date time API in java

Introduction: In this text, we can talk approximately Data time API in java. The java.time, java.util, java.sql, and java.text packages contain classes that represent dates and times. The following classes are...

4 minutes read.

Reverse a String in Java

Reversing a string means that if we have a string called “what is your name”, the reversed format is “eman ruoy si tahw”. Reversing a string involves totally flipping the...

3 minutes read.

Morris Traversal for Preorder in Java

Without the use of recursion or stacks, we traverse a tree using the Morris algorithm. The linked binary tree is the foundation of the Morris traversal. Preorder Morris Traversal Algorithm The preorder...

3 minutes read.

StringBuffer in Java

StringBuffer in Java Similar to StringBuilder, the Java StringBuffer class is also used to create modifiable or mutable strings. The StringBuilder class is synchronized, i.e., thread-safe. Java StringBuffer ConstructorThe StringBuffer class has...

7 minutes read.

Byte to Hex in Java

Java exclusively uses byte data types to store in a byte array, which is an array. Each component of a byte array has a default value of 0. Hex String -...

3 minutes read.

Java Naming Conventions

JAVA NAMING CONVENTIONS Java naming convention is a standard pattern for writing your identifier name such as class, interfaces, methods, constants, variable, etc. These patterns are not standard rules that you must...

2 minutes read.

Stack vs Heap in Java

In Java, whenever we declare an object or create a variable, whether it is an instance variable, local variable, or static variable, a certain memory is used to store the...

4 minutes read.

Instanceof operator in Java

To determine whether an object is an instance of the supplied type in Java, use the instanceof operator (class or subclass or interface). Because it compares the instance with type, the...

3 minutes read.

Comparator vs Comparable in Java

Comparator Vs Comparable in Java In Java, sorting of primitive data types can be done using different inbuilt functions that are available. But for sorting the collection of objects or types...

4 minutes read.

Default Virtual Behaviour in C++ vs Java

Virtual Behaviour in C++: The class member methods in C++ are, by default, non-virtual. This implies that by simply defining it, they can be turned into virtual. The virtual class can be...

3 minutes read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or...

4 minutes read.

Java 13 New Features

The Java SE Platform's JDK 13 version is an open-source reference implementation defined by the Java Community's JSR 388 Process. The necessary modifications made in Java 13 are listed below....

6 minutes read.

Convert list to array Java

One of the popular collection interfaces for storing an ordered collection is the List. The List interface may contain repeating groups and preserves the insertion order of entries. This article will...

4 minutes read.

Bad Operand types for Binary Operator Java

We will discuss how to handle issues in bad operand types for binary operator &, bad operand types for binary operator &&, bad operand types for binary operator ==, and...

4 minutes read.

Difference between String, StringBuffer and StringBuilder in java

What is a string in Java? Strings are a collection of characters that are commonly used in Java programming. Strings are regarded as objects in the Java programming language. “String” is a...

4 minutes read.

Java Default Keyword

The Default keyword in java programming language is used as access modifier.If any of the variable or the constructor or the methods or the classes are not assigned with the...

3 minutes read.

Minimum Number of Taps to Open to Water a Garden in Java

Problem Statement The issue is that a gardener wants to water a (single-dimensional) garden using the fewest possible tap openings. The goal is to determine the minimum necessary taps to be...

8 minutes read.

Java delete directory

The File classes in Java may symbolize a directory or a file on the system. Inside the java.io package, the Files class is accessible. The File class has several helpful...

2 minutes read.