CharArrayWriter write() method in Java with Examples Last Updated : 05 Jun, 2020 Comments Improve Suggest changes Like Article Like Report The write() method of CharArrayWriter class in Java is of three types: The write(int) method of CharArrayWriter class in Java is used to write a character to the writer in form of an integer. This write() method writes one character at a time to the CharArrayWriter.Syntax: public void write(int c) Overrides: This method overrides the write() method of Writer class.Parameters: This method accepts one parameter c that represents the integer that is to be written.Return value: This method does not return any value.Exceptions: This method does not throw any exception.Below program illustrates write(int) method in CharArrayWriter class in IO package:Program: Java // Java program to illustrate // CharArrayWriter write(int) method import java.io.*; public class GFG { public static void main(String[] args) { // Create charArrayWriter CharArrayWriter charArrayWriter = new CharArrayWriter(); // Write the character charArrayWriter.write(71); charArrayWriter.write(69); charArrayWriter.write(69); charArrayWriter.write(75); charArrayWriter.write(83); // print the charArrayWriter System.out.println( charArrayWriter.toString()); } } Output: GEEKS The write(char[], int, int) method of CharArrayWriter class in Java is used to write characters to the writer. It writes a portion of the given character array to the CharArrayWriter.Syntax: public void write(char[] c, int offset, int length) Specified By: This method is specified by the write() method of Writer class.Parameters: This method accepts three parameters: c - It represents the character array, the portion of which is to written.offset - It represents the start index in the character array.length - It represents the number of characters to be written. Return value: This method does not return any value.Exceptions: This method throws IndexOutOfBoundsException if offset is negative, or length is negative or sum of offset and length is negative or greater than the length of the given character array.Below program illustrates write(char[], int, int) method in CharArrayWriter class in IO package:Program: Java // Java program to illustrate // CharArrayWriter // write(char[], int, int) method import java.io.*; public class GFG { public static void main(String[] args) { // Create charArrayWriter CharArrayWriter charArrayWriter = new CharArrayWriter(); // Create character array char[] c = { 'G', 'E', 'E', 'K', 'S' }; // Write protion of array // to the charArrayWriter charArrayWriter.write(c, 0, 4); // print the charArrayWriter System.out.println( charArrayWriter.toString()); } } Output: GEEK The write(String, int, int) method of CharArrayWriter class in Java is used to write a portion of a string to the writer. This write() method writes a portion of the given string to the CharArrayWriter.Syntax: public void write(String str, int offset, int length) Overrides: This method overrides the write() method of Writer class.Parameters: This method accepts three parameters: str - It represents the string, the portion of which is to written.offset - It represents the start index in the string.length - It represents the number of characters to be written. Return value: This method does not return any value.Exceptions: This method throws IndexOutOfBoundsException if offset is negative, or length is negative or sum of offset and length is negative or greater than the length of the given string.Below program illustrates write(String, int, int) method in CharArrayWriter class in IO package:Program: Java // Java program to illustrate // CharArrayWriter // write(String, int, int) method import java.io.*; public class GFG { public static void main(String[] args) { // Create charArrayWriter CharArrayWriter charArrayWriter = new CharArrayWriter(); // Create string String str = "GEEKSFORGEEKS"; // Write protion of string // to the charArrayWriter charArrayWriter.write(str, 8, 5); // print the charArrayWriter System.out.println( charArrayWriter.toString()); } } Output: GEEKS References: 1. https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#write(int) 2. https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#write(char%5B%5D, int, int) 3. https://docs.oracle.com/javase/10/docs/api/java/io/CharArrayWriter.html#write(java.lang.String, int, int) Create Quiz Comment P pp_pankaj Follow 0 Improve P pp_pankaj Follow 0 Improve Article Tags : Java Java-Functions Java-IO package Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References7 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers1 min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like