GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Python Technologies
  • Databases
  • Computer Programming
  • Web Development
  • Java Technologies
  • Computer Science
  • Mobile Development
  • Big Data & Analytics
  • Microsoft Technologies
  • DevOps
  • Latest Technologies
  • Machine Learning
  • Digital Marketing
  • Software Quality

This view

  • Original page
  • New address

GeezerVison is showing www.tutorialspoint.com — Java - CharArrayWriter write(String str, int off, int len) method

Java.io Package Tutorial
  • Java.io - Home
  • Java.io - BufferedInputStream
  • Java.io - BufferedOutputStream
  • Java.io - BufferedReader
  • Java.io - BufferedWriter
  • Java.io - ByteArrayInputStream
  • Java.io - ByteArrayOutputStream
  • Java.io - CharArrayReader
  • Java.io - CharArrayWriter
  • Java.io - Console
  • Java.io - DataInputStream
  • Java.io - DataOutputStream
  • Java.io - File
  • Java.io - FileDescriptor
  • Java.io - FileInputStream
  • Java.io - FileOutputStream
  • Java.io - FilePermission
  • Java.io - FileReader
  • Java.io - FileWriter
  • Java.io - FilterInputStream
  • Java.io - FilterOutputStream
  • Java.io - FilterReader
  • Java.io - FilterWriter
  • Java.io - InputStream
  • Java.io - InputStreamReader
  • Java.io - LineNumberInputStream
  • Java.io - LineNumberReader
  • Java.io - ObjectInputStream
  • Java.io - ObjectInputStream.GetField
  • Java.io - ObjectOutputStream
  • io - ObjectOutputStream.PutField
  • Java.io - ObjectStreamClass
  • Java.io - ObjectStreamField
  • Java.io - OutputStream
  • Java.io - OutputStreamWriter
  • Java.io - PipedInputStream
  • Java.io - PipedOutputStream
  • Java.io - PipedReader
  • Java.io - PipedWriter
  • Java.io - PrintStream
  • Java.io - PrintWriter
  • Java.io - PushbackInputStream
  • Java.io - PushbackReader
  • Java.io - RandomAccessFile
  • Java.io - Reader
  • Java.io - SequenceInputStream
  • Java.io - SerializablePermission
  • Java.io - StreamTokenizer
  • Java.io - StringBufferInputStream
  • Java.io - StringReader
  • Java.io - StringWriter
  • Java.io - Writer
  • Java.io - Interfaces
  • Java.io - Exceptions
  • Java.io package Useful Resources
  • Java.io - Discussion
Selected Reading
  • UPSC IAS Exams Notes
  • Developer's Best Practices
  • Questions and Answers
  • Online Resume Builder
  • HR Interview Questions
  • Computer Glossary
  • Who is Who

Java - CharArrayWriter write(String str, int off, int len) method



Previous
Quiz
Next

Description

The Java CharArrayWriter write(String str, int off, int len) method writes a part of the string to the writer.

Declaration

Following is the declaration for java.io.CharArrayWriter.write(String str, int off, int len) method −

public void write(String str, int off, int len)

Parameters

  • str − Source string.

  • off − Offset to start reading the characters from.

  • len − Number of characters to be written.

Return Value

This method does not return any value.

Exception

NA

Example - Usage of write(String str, int off, int len) method

The following example shows the usage of Java CharArrayWriter write(String str, int off, int len) method.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {      
      String str = "Hello World!!";
      CharArrayWriter chw = null;
            
      try {
         // create character array writer
         chw = new CharArrayWriter();

         // portion to be written to writer
         chw.write(str, 4, 9);
         
         // print the buffer as string
         System.out.println(chw.toString());
               
      } catch(Exception e) {
         // for any error
         e.printStackTrace();
      } finally {
         // releases all system resources from writer
         if(chw!=null)
            chw.close();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

o World!!

Example - Writing a Substring to CharArrayWriter

The following example shows the usage of Java CharArrayWriter write(String str, int off, int len) method.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {
      CharArrayWriter writer = new CharArrayWriter();
      String sentence = "Welcome to Java Programming!";

      // Write "Welcome" (from index 0, length 7)
      writer.write(sentence, 0, 7);

      // Write "Java" (from index 11, length 4)
      writer.write(sentence, 11, 4);

      // Write "!" (from index 26, length 1)
      writer.write(sentence, 26, 1);

      // Convert to string and print
      System.out.println("Written data: " + writer.toString());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Written data: WelcomeJavag

Explanation

  • The original string is "Welcome to Java Programming!".

  • write(String s, int off, int len) is called three times to write−

    • "Welcome" from index 0 (length 7).

    • "Java" from index 11 (length 4).

    • "!" from index 26 (length 1).

  • The toString() method converts the contents into a string and prints it.

Example - Writing a Partial String from the Middle

The following example shows the usage of Java CharArrayWriter write(String str, int off, int len) method.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {
      CharArrayWriter writer = new CharArrayWriter();
      String message = "Java Programming Language";

      // Write substring "Programming" (from index 5, length 11)
      writer.write(message, 5, 11);

      // Convert to string and print
      System.out.println("Written data: " + writer.toString());
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Written data: Programming

Explanation

  • A string "Java Programming Language" is used as input.

  • The write(String s, int off, int len) method writes "Programming" (starting at index 5 with length 11).

  • The final output is obtained using toString().

java_io_chararraywriter.htm
Tutorix - AI Tutor
Previous Quiz Next
Advertisements
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.