Seek file pointer position in binary file with RandomAccessFile in Java

Description

The following code shows how to seek file pointer position in binary file with RandomAccessFile.

Example


  //from   w  w w . ja va2 s . co m

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

public class Main {
  public static void main(String[] args) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(new File("temp.tmp"), "rw");
    raf.writeInt(1);
    for (int i = 0; i < 10; i++) {
      raf.seek(raf.length() - 4);
      raf.writeInt(raf.readInt());
    }
    raf.close();

  }
}




















Home »
  Java Tutorial »
    I/O »




Binary File
Byte Array
CharSet
Checksum
Console
Create Copy Move Delete
Directory
Drive
Encode Decode
File Attribute
File Lock
File System
GZIP
Jar File
NIO Buffer
Path
Scanner
StreamTokenizer
Temporary File
Text File
Zip