Using a Random Access File - Java File Path IO

Java examples for File Path IO:RandomAccessFile

Description

Using a Random Access File

Demo Code

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

public class Main {
  public static void main(String[] args) throws Exception {
    try {/*  w  ww.jav  a 2 s  .c  om*/
      File f = new File("filename");
      RandomAccessFile raf = new RandomAccessFile(f, "rw");

      // Read a character
      char ch = raf.readChar();

      // Seek to end of file
      raf.seek(f.length());

      // Append to the end
      raf.writeChars("aString");
      raf.close();
    } catch (IOException e) {
    }
  }
}

Related Tutorials