Java RandomAccessFile.seek(long pos)
Syntax
RandomAccessFile.seek(long pos) has the following syntax.
public void seek(long pos) throws IOException
Example
In the following code shows how to use RandomAccessFile.seek(long pos) method.
//ww w .ja v a2 s .c om
import java.io.*;
public class Main {
public static void main(String[] args) {
try {
RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");
raf.writeUTF("Hello World from java2s.com");
// set the file pointer at 0 position
raf.seek(0);
// print the string
System.out.println(raf.readUTF());
// set the file pointer at 5 position
raf.seek(5);
// write something in the file
raf.writeUTF("This is an example from java2s.com");
// set the file pointer at 0 position
raf.seek(0);
System.out.println(raf.readUTF());
raf.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
Home »
Java Tutorial »
java.io »
Java Tutorial »
java.io »