
- 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 package Useful Resources
- Java.io - Discussion
Java - Writer append(CharSequence csq) method
Description
The Java Writer append(CharSequence csq) method appends the specified character sequence to this writer.
Declaration
Following is the declaration for java.io.Writer.append(CharSequence csq) method.
public Writer append(CharSequence csq)
Parameters
csq − The character sequence to append. If csq is null, then the four characters "null" are appended to this writer.
Return Value
This method returns the current writer.
Exception
IOException − If an I/O error occurs.
Example - Usage of Writer append(CharSequence csq) method
The following example shows the usage of Writer append(CharSequence csq) method.
WriterDemo.java
package com.tutorialspoint; import java.io.IOException; import java.io.PrintWriter; import java.io.Writer; public class WriterDemo { public static void main(String[] args) { CharSequence csq = "Hello World!"; // create a new writer Writer writer = new PrintWriter(System.out); try { // append a sequence and change line writer.append("This is an example\n"); // flush the writer writer.flush(); // append a new sequence writer.append(csq); // flush the writer to see the result writer.flush(); } catch (IOException ex) { ex.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result −
This is an example Hello World!
Example - Append a String
The following example shows the usage of Writer append(CharSequence csq) method.
WriterDemo.java
package com.tutorialspoint; import java.io.StringWriter; public class WriterDemo { public static void main(String[] args) { StringWriter writer = new StringWriter(); CharSequence csq = "Hello, World!"; writer.append(csq); System.out.println("Output: " + writer.toString()); } }
Output
Let us compile and run the above program, this will produce the following result−
Output: Hello, World!
Explanation
"Hello, World!" (a String, which is a CharSequence) is appended to the writer.
The entire string is stored in the buffer.
Example - Append a StringBuilder and Handle null
The following example shows the usage of Writer append(CharSequence csq) method.
WriterDemo.java
package com.tutorialspoint; import java.io.StringWriter; public class WriterDemo { public static void main(String[] args) { StringWriter writer = new StringWriter(); StringBuilder sb = new StringBuilder("Java"); writer.append(sb); writer.append(" is fun!"); CharSequence nullSeq = null; writer.append(nullSeq); // Will append "null" System.out.println("Final Output: " + writer.toString()); } }
Output
Let us compile and run the above program, this will produce the following result−
Final Output: Java is fun!null
Explanation
Appends "Java" from a StringBuilder.
Then appends " is fun!".
Then appends a null CharSequence, which results in "null" being added.