
- 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 - FileWriter write(int c) method
Description
The Java FileWriter write(int c) method writes a single character to a file. The integer f represents the ASCII value of the character to be written.
Declaration
Following is the declaration for java.io.FileWriter.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 any value.
Exception
IOException − If an I/O error occurs.
Example - Writing a Single Character to a File
The following example shows the usage of Java FileWriter write(int c) method.
FileWriterDemo.java
package com.tutorialspoint; import java.io.FileWriter; import java.io.IOException; public class FileWriterDemo { public static void main(String[] args) { try (FileWriter fileWriter = new FileWriter("output.txt")) { fileWriter.write(65); // Writing 'A' (ASCII 65) System.out.println("Character 'A' written to file."); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } } }
Output
Let us compile and run the above program, this will produce the following result−
A
Explanation
Opens output.txt in write mode (overwrites existing content).
Writes ASCII character 65, which corresponds to 'A'.
Closes the file automatically using try-with-resources.
Example - Writing Multiple Characters Using a Loop
The following example shows the usage of Java FileWriter write(int c) method.
FileWriterDemo.java
package com.tutorialspoint; import java.io.FileWriter; import java.io.IOException; public class FileWriterDemo { public static void main(String[] args) { try (FileWriter fileWriter = new FileWriter("output.txt")) { for (int i = 97; i <= 101; i++) { // ASCII 97-101 -> 'a' to 'e' fileWriter.write(i); } System.out.println("Characters 'a' to 'e' written to file."); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } } }
Output
Let us compile and run the above program, this will produce the following result−
Characters 'a' to 'e' written to file.
Explanation
Writes ASCII values 97 to 101, which correspond to 'a' to 'e'.
Uses a loop to write multiple characters one by one.
Example - Appending a Character to an Existing File
The following example shows the usage of Java FileWriter write(int c) method.
FileWriterDemo.java
package com.tutorialspoint; import java.io.FileWriter; import java.io.IOException; public class FileWriterDemo { public static void main(String[] args) { try (FileWriter fileWriter = new FileWriter("output.txt", true)) { // true enables append mode fileWriter.write(90); // Writing 'Z' (ASCII 90) at the end System.out.println("Character 'Z' appended to file."); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); } } }
Output
Let us compile and run the above program, this will produce the following result−
Character 'Z' appended to file.
Explanation
Opens output.txt in append mode (true in FileWriter constructor).
Writes ASCII 90, which is 'Z', at the end of the file.