- 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 write(string str) method
Description
The Java Writer write(string str) method writes a string.
Declaration
Following is the declaration for java.io.Writer.write(string str) method.
public void write(string str)
Parameters
str − String to be written.
Return Value
This method does not return a value.
Exception
IOException − If an I/O error occurs.
Example - Usage of Writer write(string str) method
The following example shows the usage of Writer write(string str) 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) {
String str = "Hello world!";
// create a new writer
Writer writer = new PrintWriter(System.out);
try {
// write a string
writer.write(str);
// flush the writer
writer.flush();
// change line and write another string
writer.write("\nThis is an example");
// flush the stream again
writer.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Output
Let us compile and run the above program, this will produce the following result −
Hello world! This is an example
Example - Using FileWriter with write(String s)
The following example shows the usage of Writer write(string str) method.
WriterDemo.java
package com.tutorialspoint;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
public class WriterDemo {
public static void main(String[] args) {
try {
Writer writer = new FileWriter("write_string1.txt");
String message = "Hello, Java Writer!";
writer.write(message); // Writes the full string to the file
writer.close();
System.out.println("String written to file.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
Let us compile and run the above program, this will produce the following result−
Output: Writing to memory using StringWriter.
Explanation
A String is written using write(String s) to a file.
FileWriter is used to output text to write_string1.txt.
After writing, the writer is closed.
Example - Using StringWriter with write(String s)
The following example shows the usage of Writer write(string str) method.
WriterDemo.java
package com.tutorialspoint;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
public class WriterDemo {
public static void main(String[] args) {
try {
Writer writer = new StringWriter();
String data = "Writing to memory using StringWriter.";
writer.write(data); // Writes the entire string to memory buffer
System.out.println("Output: " + writer.toString());
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output
Let us compile and run the above program, this will produce the following result−
Output: Writing to memory using StringWriter.
Explanation
A string is written into an internal buffer using StringWriter.
The contents are displayed using writer.toString() instead of writing to a file.