Java - RandomAccessFile writeBytes(String s) method



Description

The Java RandomAccessFile writeBytes(String s) method writes the string to the file as a sequence of bytes. Each character in the string is written out, in sequence, by discarding its high eight bits. The write starts at the current position of the file pointer.

Declaration

Following is the declaration for java.io.RandomAccessFile.writeBytes(String s) method.

public final void writeBytes(String s)

Parameters

s − a string of bytes to be written.

Return Value

This method does not return a value.

Exception

  • IOException − If an I/O error occurs.

Example - Usage of RandomAccessFile writeBytes(String s) method

The following example shows the usage of RandomAccessFile writeBytes(String s) method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
   
      try {
         // create a new RandomAccessFile with filename test
         RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");

         // write bytes in the file
         raf.writeBytes("Hello World");

         // set the file pointer at 0 position
         raf.seek(0);

         // read line
         System.out.println(raf.readLine());

         // set the file pointer at 0 position
         raf.seek(0);

         // write bytes at the start
         raf.writeBytes("This is an example");

         // set the file pointer at 0 position
         raf.seek(0);

         // read line
         System.out.println(raf.readLine());
         
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

Assuming we have a text file test.txt in current directory which has the following content. This file will be used as an input for our example program −

ABCDE

Let us compile and run the above program, this will produce the following result −

Hello World
This is an example

Example - Writing a String to a File

The following example shows the usage of RandomAccessFile writeBytes(String s) method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
      try {
         RandomAccessFile raf = new RandomAccessFile("example1.txt", "rw");

         raf.writeBytes("Hello, world!");
         raf.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−

String written to file.

Explanation

  • This writes the string "Hello, world!" to a file named example1.txt.

  • writeBytes(String s) writes the low-order 8 bits (i.e., the byte value) of each character in the string.

  • It doesn't handle Unicode or UTF-8-just straight 1-byte-per-character encoding (ISO-8859-1 / Latin-1 style).

  • Great for simple ASCII content, but not suitable for characters outside the 0-255 range (e.g., emoji, Asian scripts).

Example - Appending Text to an Existing File

The following example shows the usage of RandomAccessFile writeBytes(String s) method.

RandomAccessFileDemo.java

package com.tutorialspoint;

import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessFileDemo {
   public static void main(String[] args) {
      try {
         RandomAccessFile raf = new RandomAccessFile("example2.txt", "rw");

         // Move file pointer to the end of the file
         raf.seek(raf.length());

         // Append text
         raf.writeBytes("\nAppended line.");

         raf.close();

         System.out.println("Text appended to file.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result−

Text appended to file.

Explanation

  • This opens example2.txt in read-write mode and moves the pointer to the end of the file using raf.seek(raf.length()).

  • Then it appends the string "\nAppended line." to the file.

  • This is how you simulate append mode with RandomAccessFile, since it doesn't have a built-in append flag.

java_io_randomaccessfile.htm