Java RandomAccessFile .writeChars (String s)

Syntax

RandomAccessFile.writeChars(String s) has the following syntax.

public final void writeChars(String s)   throws IOException

Example

In the following code shows how to use RandomAccessFile.writeChars(String s) method.


//from  ww w  .  ja  v a 2  s  .c  om
import java.io.*;

public class Main {

   public static void main(String[] args) {
      try {
         String s = "Hello World from java2s.com";
         
         RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

         raf.writeChars(s);

         raf.seek(0);

         for (int i = 0; i < 15; i++) {
            System.out.println(raf.readChar());
         }

         raf.seek(0);

         raf.writeChars("This is an example from java2s.com");

         raf.seek(0);

         for (int i = 0; i < 20; i++) {
            System.out.println(raf.readChar());
         }
         raf.close();
      } catch (IOException ex) {
         ex.printStackTrace();
      }

   }
}