GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — Java - CharArrayWriter Class
Selected Reading
Java - CharArrayWriter ClassIntroductionThe Java CharArrayWriter class implements a character buffer that can be used as an Writer. The buffer automatically grows when data is written to the stream. Class declarationFollowing is the declaration for Java.io.CharArrayWriter class − public class CharArrayWriter extends Writer FieldFollowing are the fields for Java.io.CharArrayWriter class −
Class constructors
Class methods
Methods inheritedThis class inherits methods from the following classes −
Example - Appending a Single Character to a CharArrayWriterThe following example shows the usage of Java CharArrayWriter append(char c) method. CharArrayWriterDemo.javapackage com.tutorialspoint;
import java.io.CharArrayWriter;
public class CharArrayWriterDemo {
public static void main(String[] args) {
// Creating a CharArrayWriter instance
CharArrayWriter charArrayWriter = new CharArrayWriter();
// Appending characters one by one
charArrayWriter.append('H');
charArrayWriter.append('i');
charArrayWriter.append('!');
// Converting the CharArrayWriter content to a string and displaying it
System.out.println("CharArrayWriter content: " + charArrayWriter.toString());
// Closing the CharArrayWriter (not necessary, but good practice)
charArrayWriter.close();
}
}
OutputLet us compile and run the above program, this will produce the following result − CharArrayWriter content: Hi! Explanation
Example - Appending a Subsequence from a StringBuilderThe following example shows the usage of Java CharArrayWriter append(CharSequence csq, int start, int end) method. CharArrayWriterDemo.javapackage com.tutorialspoint;
import java.io.CharArrayWriter;
public class CharArrayWriterDemo {
public static void main(String[] args) {
CharArrayWriter writer = new CharArrayWriter();
// Create a StringBuilder
StringBuilder sb = new StringBuilder("Spring Boot Framework");
// Append a substring from index 7 to 11 ("Boot")
writer.append(sb, 7, 11);
// Append another substring from index 12 to 21 ("Framework")
writer.append(sb, 12, 21);
// Print the output
System.out.println(writer.toString()); // Output: BootFramework
}
}
OutputLet us compile and run the above program, this will produce the following result − BootFramework Explanation
Example - Closing a CharArrayWriter and Writing Data After ClosingThe following example shows the usage of Java CharArrayWriter close() method. CharArrayWriterDemo.javapackage com.tutorialspoint;
import java.io.CharArrayWriter;
import java.io.IOException;
public class CharArrayWriterDemo {
public static void main(String[] args) throws IOException {
CharArrayWriter writer = new CharArrayWriter();
// Write data to CharArrayWriter
writer.write("Hello, Java!");
// Close the writer
writer.close();
// Append more data after closing (allowed)
writer.append(" Still working!");
// Convert to string and print
System.out.println(writer.toString()); // Output: Hello, Java! Still working!
}
}
OutputLet us compile and run the above program, this will produce the following result − Hello, Java! Still working! Explanation
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. |