
- 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(int c) method
Description
The Java Writer write(int c) method writes a single character. The character to be written is contained in the 16 low-order bits of the given integer value; the 16 high-order bits are ignored.
Declaration
Following is the declaration for java.io.Writer.write(int c) method.
public void write(int c)
Parameters
c − int specifying a character 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(int c) method
The following example shows the usage of Writer write(int c) 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) { int c = 70; // create a new writer Writer writer = new PrintWriter(System.out); try { // write an int that will be printed as ASCII writer.write(c); // flush the writer writer.flush(); // write another int that will be printed as ASCII writer.write(71); // 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 −
FG
Example - Using FileWriter with write(int c)
The following example shows the usage of Writer write(int c) 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("writeint1.txt"); writer.write(65); // Writes character 'A' (Unicode value 65) writer.write(66); // Writes character 'B' writer.write(67); // Writes character 'C' writer.close(); System.out.println("Characters written to file."); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Characters written to file.
Explanation
Unicode values 65, 66, and 67 correspond to characters 'A', 'B', and 'C'.
write(int) writes one character at a time.
Output file will contain: ABC.
Example - Using StringWriter with write(int c)
The following example shows the usage of Writer write(int c) 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(); int[] charCodes = {72, 101, 108, 108, 111}; // Corresponds to 'H', 'e', 'l', 'l', 'o' for (int code : charCodes) { writer.write(code); } System.out.println("Output: " + writer.toString()); // Prints "Hello" writer.close(); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Output: Hello
Explanation
An array of character codes (Unicode values) is written one by one.
StringWriter holds the result in memory, which we print using toString().