Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
Java.io.Printstream Class in Java | Set 1
Next article icon

Java.io.Writer class in Java

Last Updated : 12 Sep, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

This abstract class for writing to character streams. The only methods that a subclass must implement are write(char[], int, int), flush(), and close(). Most subclasses, however, will override some of the methods defined here in order to provide higher efficiency, additional functionality, or both.
Constructor

  • protected Writer() : Creates a new character-stream writer whose critical sections will synchronize on the writer itself.
  • protected Writer(Object lock) : Creates a new character-stream writer whose critical sections will synchronize on the given object.

Methods:

  • Writer append(char c) : Appends the specified character to this writer.An invocation of this method of the form out.append(c) behaves in exactly the same way as the invocation
    out.write(c)
    Syntax :public Writer append(char c)                throws IOException  Parameters:  c - The 16-bit character to append  Returns:  This writer  Throws:  IOException
  • Writer append(CharSequence csq) : Appends the specified character sequence to this writer.An invocation of this method of the form out.append(csq) behaves in exactly the same way as the invocation
    out.write(csq.toString())
    Depending on the specification of toString for the character sequence csq, the entire sequence may not be appended. For instance, invoking the toString method of a character buffer will return a subsequence whose content depends upon the buffer’s position and limit.
    Syntax :public Writer append(CharSequence csq)                throws IOException  Parameters:  csq - The character sequence to append. If csq is null,   then the four characters "null" are appended to this writer.  Returns:  This writer  Throws:  IOException
  • Writer append(CharSequence csq, int start, int end) : Appends a subsequence of the specified character sequence to this writer.Appends a subsequence of the specified character sequence to this writer
    Syntax :public Writer append(CharSequence csq,              int start,              int end)                throws IOException  Parameters:  csq - The character sequence from which a subsequence will be appended.   If csq is null, then characters will be appended as   if csq contained the four characters "null".  start - The index of the first character in the subsequence  end - The index of the character following the last character in the subsequence  Returns:  This writer  Throws:  IndexOutOfBoundsException  IOException
  • abstract void close() : Closes the stream, flushing it first. Once the stream has been closed, further write() or flush() invocations will cause an IOException to be thrown. Closing a previously closed stream has no effect.
    Syntax :public abstract void close()                      throws IOException  Throws:  IOException 
  • abstract void flush() : Flushes the stream.If the stream has saved any characters from the various write() methods in a buffer, write them immediately to their intended destination. Then, if that destination is another character or byte stream, flush it. Thus one flush() invocation will flush all the buffers in a chain of Writers and OutputStreams.
    Syntax :public abstract void flush()                      throws IOException  Throws:  IOException
  • void write(char[] cbuf) : Writes an array of characters.
    Syntax :public void write(char[] cbuf)             throws IOException  Parameters:  cbuf - Array of characters to be written  Throws:  IOException - If an I/O error occurs
  • abstract void write(char[] cbuf, int off, int len) : Writes a portion of an array of characters.
    Syntax :public abstract void write(char[] cbuf,           int off,           int len)                      throws IOException  Parameters:  cbuf - Array of characters  off - Offset from which to start writing characters  len - Number of characters to write  Throws:  IOException
  • void write(int c) : Writes a single character.The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.
    Subclasses that intend to support efficient single-character output should override this method.
    Syntax :public void write(int c)             throws IOException  Parameters:  c - int specifying a character to be written  Throws:  IOException
  • void write(String str) : Writes a string.
    Syntax :public void write(String str)             throws IOException  Parameters:  str - String to be written  Throws:  IOException
  • void write(String str, int off, int len) : Writes a portion of a string.
    Syntax :public void write(String str,           int off,           int len)             throws IOException  Parameters:  str - A String  off - Offset from which to start writing characters  len - Number of characters to write  Throws:  IndexOutOfBoundsException 

Program :




//Java program demonstrating Writer methods
  
import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;
class WriterDemo
{
    public static void main(String[] args) throws IOException
    {
        Writer wr=new PrintWriter(System.out);
        char c[] = {'B','C','D','E','F'};
        CharSequence cs = "JKL";
        String str = "GHI";
  
        //illustrating write(int c)
        wr.write(65);
          
        //flushing the stream
        wr.flush();
          
        //illustrating write(char[] c,int off,int len)
        wr.write(c);
        wr.flush();
          
        //illustrating write(String str,int off,int len)
        wr.write(str);
        wr.flush();
          
        //illustrating append(Charsequence cs,int start,int end)
        wr.append(cs);
        wr.flush();
          
        //illustrating append(int ch)
        wr.append('M');
        wr.flush();
  
        //closing the stream
        wr.close();
    }
}
 
 

Output :

ABCDEFGHIJKLM


Next Article
Java.io.Printstream Class in Java | Set 1

N

Nishant Sharma
Improve
Article Tags :
  • Java
Practice Tags :
  • Java

Similar Reads

  • Java.io.StringWriter class in Java
    Java StringWriter class creates a string from the characters of the String Buffer stream. Methods of the StringWriter class in Java can also be called after closing the Stream as this will raise no IO Exception. Declaration in Java StringWriter Classpublic class StringWriter extends WriterConstructo
    6 min read
  • Java.io.PrintWriter class in Java | Set 1
    Java PrintWriter class gives Prints formatted representations of objects to a text-output stream. It implements all of the print methods found in PrintStream. It does not contain methods for writing raw bytes, for which a program should use unencoded byte streams. Unlike the PrintStream class, if au
    5 min read
  • Java.io.PrintWriter class in Java | Set 2
    Java.io.PrintWriter class in Java | Set 1 More methods: PrintWriter printf(Locale l, String format, Object... args) : A convenience method to write a formatted string to this writer using the specified format string and arguments. Syntax :public PrintWriter printf(Locale l, String format, Object...
    7 min read
  • Java.io.StringReader class in Java
    StringReader class in Java is a character stream class whose source is a string. It inherits Reader Class. Closing the StringReader is not necessary, it is because system resources like network sockets and files are not used. Let us check more points about StringReader Class in Java. Declare StringR
    4 min read
  • Java.io.Printstream Class in Java | Set 1
    A PrintStream adds functionality to another output stream, namely the ability to print representations of various data values conveniently. Unlike other output streams, a PrintStream never throws an IOException; instead, exceptional situations merely set an internal flag that can be tested via the c
    5 min read
  • Java.io.Printstream Class in Java | Set 2
    Java.io.Printstream Class in Java | Set 1More Methods: PrintStream printf(Locale l, String format, Object... args) : A convenience method to write a formatted string to this output stream using the specified format string and arguments. Syntax :public PrintStream printf(Locale l, String format, Obje
    6 min read
  • Java.io.OutputStream class in Java
    This abstract class is the superclass of all classes representing an output stream of bytes. An output stream accepts output bytes and sends them to some sink. Applications that need to define a subclass of OutputStream must always provide at least a method that writes one byte of output. Constructo
    2 min read
  • Java Writer Class
    Java writer class is an abstract class in the java.io package. It is designed for writing character streams. Writer class in Java provides methods for writing characters, arrays of characters, and strings. Since it is an abstract class, we cannot create an instance of it directly. Instead, we will u
    5 min read
  • Java.lang.Runtime class in Java
    In Java, the Runtime class is used to interact with Every Java application that has a single instance of class Runtime that allows the application to interface with the environment in which the application is running. The current runtime can be obtained from the getRuntime() method. Methods of Java
    6 min read
  • Java.io.Console class in Java
    The Java.io.Console class provides methods to access the character-based console device, if any, associated with the current Java virtual machine. The Console class was added to java.io by JDK 6. Important Points: It is used to read from and write to the console, if one exists. Console is primarily
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences