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.

java_io_filewriter.htm
Advertisements