Java IO Tutorial - Java RandomAccessFile .writeUTF (String str)








Syntax

RandomAccessFile.writeUTF(String str) has the following syntax.

public final void writeUTF(String str)   throws IOException

Example

In the following code shows how to use RandomAccessFile.writeUTF(String str) method.

/*from ww w  .  ja va 2 s. c  o  m*/
import java.io.RandomAccessFile;

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.writeUTF(s);

         raf.seek(0);

         System.out.println(raf.readUTF());

         raf.seek(0);

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

         raf.seek(0);

         System.out.println(raf.readUTF());
         raf.close();
      } catch (Exception ex) {
         ex.printStackTrace();
      }

   }
}