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 write(String str, int off, int len) method
Selected Reading
Java - CharArrayWriter write(String str, int off, int len) methodDescriptionThe Java CharArrayWriter write(String str, int off, int len) method writes a part of the string to the writer. DeclarationFollowing 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
Return ValueThis method does not return any value. ExceptionNA Example - Usage of write(String str, int off, int len) methodThe following example shows the usage of Java CharArrayWriter write(String str, int off, int len) method. CharArrayWriterDemo.javapackage 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();
}
}
}
OutputLet us compile and run the above program, this will produce the following result − o World!! Example - Writing a Substring to CharArrayWriterThe following example shows the usage of Java CharArrayWriter write(String str, int off, int len) method. CharArrayWriterDemo.javapackage 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());
}
}
OutputLet us compile and run the above program, this will produce the following result − Written data: WelcomeJavag Explanation
Example - Writing a Partial String from the MiddleThe following example shows the usage of Java CharArrayWriter write(String str, int off, int len) method. CharArrayWriterDemo.javapackage 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());
}
}
OutputLet us compile and run the above program, this will produce the following result − Written data: Programming Explanation
java_io_chararraywriter.htm 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. |