×

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

Camel Case in Java

Java names its classes, interfaces, methods, as well as variables using camel-case syntax. If the name consists of two words, the second word will always begin with a capital letter,...

3 minutes read.

Java Math ceil() Method

The ceil() method of Math class returns the smallest double value which is closest to negative infinity and is greater than or equal to the argument. Syntax: public static double ceil(double a) Parameters: The...

2 minutes read.

Mutable and Immutable in Java

Java is a programming language in which everything is treated as an object. Its procedures and functions are centred around objects because it is an object-oriented programming language. Mutable and...

6 minutes read.

How to find length of integer in Java

We can find the length of the integer in many ways. The length of an integer is defined as the count of the number of digits for the given integer. These...

5 minutes read.

Java Integer numberOfLeadingZeros() method

The numberOfLeadingZeros()  method of Java Integer class returns the total number of zero bits preceding the highest-order one-bit in the 2’s complement binary representation of the specified int value.  Syntax public static...

1 minute read.

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.Constructor chaining is the process of calling one constructor from another constructor using the same object....

2 minutes read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

Java Externalization

What is Externalization in Java? Externalization is a concept that is advanced to serialization. Serialization is a concept where we can transfer an object from our JVM ( Java virtual machine...

3 minutes read.

Java Integer toUnsignedString() method

The toUnsignedString() method of Java Integer class returns a string representation of the argument as an unsigned decimal value. The second syntax returns a string representation of the given argument as...

2 minutes read.

Enterprise Java Beans

One of the many Java APIs for the common development of corporate software is Enterprise Java Beans (EJB). An EJB, a server-side software component, contains the business logic of an...

4 minutes read.

How to add 6 Months to the Current Date in Java?

In this tutorial, we will learn how to add 6 months to the local or current date in Java language. We will begin our topic with basic concepts and would...

3 minutes read.

Java Editors

A straightforward text editor may be used to create Java applications. However, a Java integrated programming environment (IDE) enables the software developer to create programs more quickly. An IDE offers...

4 minutes read.

How to write basic Java Programs

Java Basic Programs In this section, we will learn how to write basic Java programs. But first we need to take care of the following requirement list. To execute a Java program,...

4 minutes read.

Anonymous Function in Java

A function defined as being unbound from an identifier is called an anonymous function. Because they permit access to variables within the scope of the contained function, these are a...

4 minutes read.

Java Boyer Moore

A string searching or matching technique called the Boyer-Moore algorithm was created in 1977 by Robert S. Boyer and J. Strother Moore. It is the most popular and effective string-matching...

9 minutes read.

Java Integer toString() method

The toString() method of Java Integer class returns a String object which represents this Integer’s value. The second syntax returns a String object which represents the specified integer. The third syntax returns...

2 minutes read.

Java Math nextAfter() Method

The nextAfter() method of Math class returns the floating-point value adjacent to the first argument in direction of the second argument. Syntax: public static double nextAfter (double start, double direction)public static float...

2 minutes read.

Upcasting and Downcasting in Java

Type casting in Java is an important and very interesting topic to deal with. But here upcasting and downcasting is somewhat related to typecasting. In normal typecasting, we convert from...

6 minutes read.

How to Convert Integer to String in Java

How to Convert int to String in Java It is used when you want to convert an integer to String. You can convert int to String by using the following methods: Using...

3 minutes read.

Delete a Cycle from a Linked List in Java

Assume a method detectAndRemoveLoop(), which examines if a given Linked List includes a loop and, if so, eliminates this Loop and returns true. This returns false if the list does...

7 minutes read.