Java.io - SPLessons

Java.io PrintWriter

Chapter 42

SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io PrintWriter

Java.io PrintWriter

shape Introduction

The print writer is the class used to write the data which is formatted, to underlying writer. The data by print writer is formatted in text including all primitive data types like int, byte, float, double. Any bytes of data formatted into text in case of print write and then it written to writer class. By calling close() method you can close the print writer. There are two significant methods involved in print writer class, they are

format()

printf().

Class Declaration

shape Declaration

Java.io PrintWriter class is declared below:

Public class PrintWriter extends Writer

shape Fields

Java.io PrintWriter class fields are:

  • Protected Object Lock : For synchronize operations this object is used in writer class.
  • Protected Writer out : The field represents the character output stream.

Class Constructors

shape Table

Constructor Description
PrintWriter(File file) The function of this constructor is to create a print writer with represented file and without flushing automatically.
PrintWriter(OutputStream out) The function of this constructor is to create a print writer .
PrintWriter(String fileName) The function of this constructor is to create a print writer with represented file name and without flushing automatically.
PrintWriter(OutputStream out, Boolean autoFlush) The function of this constructor is to create a print writer with specified boolean auto flush.

Class Methods

shape Table

Method Description
void close () The function of this method is to close the present stream and associated to input stream systems resources are released without any impact.
void flush () The function of this method is to flush the specified output stream.
void write (int c) The function of this method is to write the represented character into output stream.
void write (Char[] buf, int off, int n) By using this method it writes the data up to ‘n’ bytes from the output stream to an array of characters.
void println () The function of this method is to terminate the present line.
void print (object obj) The function of this method is to return the object.
void print(boolean b) The function of this method is to the boolean value.
printWriter append(Char c) The function of this method is to append the represented character for the output stream.

Inherited Methods

shape Description

From the following classes, methods are inherited to print writer class.

  • Java.io.Object

shape Examples

Usage of void write (Char[] buf, int off, int n) method.

import java.io.*;
public class PrintWriter
{
   public static void main(String[] args) 
   {
      char[] c = {'S', 'P', 'L', 'E', 'S', 'S', 'O', 'N', 'S'};

      // creating a new writer instance
      PrintWriter pw = new PrintWriter(System.out);

      // To write characters 
      pw.write(c, 0, 2);
      pw.write(c, 2, 7);

      // To flush the writer
      pw.flush();
   }
}

Output
The result will be as follows.

SPLESSONS

Usage of printWriter append(Char c) method.

import java.io.*;
public class PrintWriter
{
   public static void main(String[] args) 
   {
      try {
         // creating a new stream at system
         PrintWriter pw = new PrintWriter(System.out);
         // To append characters
         pw.append('S');
         pw.append('P');
         pw.append('L');
         pw.append('E');
         pw.append('S');
         pw.append('S');
         pw.append('O');
         pw.append('N');
         pw.append('S');
         // To flush the writer
         pw.flush();

      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output
The result will be as follows.

SPLESSONS

Summary

shape Key Points

  • The print writer is the class used to write the data which is formatted, to underlying writer.
  • The data by print writer is formatted in text including all primitive data types like int, byte, float, double.
  • Any bytes of data formatted into text in case of print write and then it written to writer class.