Java.io - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.io Writer

Java.io Writer

shape Introduction

The Java.io Writer is the class used for writing the content in the form of text like characters rather than bytes. TheJava.io Writer is the super class to all the writer classes available in java. The writer is similar to that output stream class the only difference is that writer is used to write the characters where as output stream writes the bytes of data.

shape Conceptual figure

Class Declaration

shape Declaration

Java.io Writer class is declared below: Public class Writer extends Object implements Flushable, Appendable, Closeable

shape Fields

Java.io Writer class fields are:
  • Protected Object Lock : For synchronize operations this object is used in writer class.

Class Constructors

shape Table

Constructor Description
Protected writer () The function of this constructor is to create the writer instance and used to write character stream.
Protected Writer(Object lock) The function of this constructor is to create the writer instance and synchronize the sections in the given object.

Class Methods

shape Table

Method Description
abstract void close () The function of this method is to close the present stream and associated to input stream systems resources are released.
abstract 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.
abstract void write (Char[] cbuf, int off, int n) By using this method it writes the data up to ‘n’ length from the output stream to an array of Characters.
write append (CharacterSequence cs) The function of this method is to write the represented append character sequence into output stream.

Inherited Methods

shape Description

From the following classes, methods are inherited to the writer class.
  • Java.io.object

shape Examples

Usage of void write (int c) method. [c]package com.cp.io; import java.io.*; public class Writer { public static void main(String[] args) throws IOException { int c = 83; // creating a new writer instance PrintWriter writer = new PrintWriter(System.out); // write an integer value that is printed as ASCII writer.write(c); // To flush the writer writer.flush(); // write another integer value that is printed as ASCII writer.write(80); // To flush the stream writer.flush(); // write another integer value that is printed as ASCII writer.write(76); // To flush the stream writer.flush(); } } [/c] Output The result will be as follows. [c]SPL[/c] Usage of abstract void write (Char[] cbuf, int off, int n) method. [c] package com.cp.io; import java.io.*; public class Writer { public static void main(String[] args) throws IOException { char[] c = {'S', 'P', 'L', 'E', 'S', 'S', 'O', 'N', 'S'}; // Creating a new writer instance PrintWriter writer = new PrintWriter(System.out); // To write a portion of a char array writer.write(c, 0, 5); // To flush the writer writer.flush(); // write remaining portion of a char array writer.write(c, 4, 4); // To flush the stream writer.close(); } }[/c] Output The result will be as follows. [c]SPLESSSON[/c]

Summary

shape Key Points

  • The Java.io Writer is the class used for writing the content in the form of text like characters rather than bytes.
  • The writer is the super class to all the writer classes available in java.
  • The writer is similar to that output stream class the only difference is that writer is used to write the characters where as output stream writes the bytes of data.