Example usage for java.io RandomAccessFile seek

List of usage examples for java.io RandomAccessFile seek

Introduction

In this page you can find the example usage for java.io RandomAccessFile seek.

Prototype

public void seek(long pos) throws IOException 

Source Link

Document

Sets the file-pointer offset, measured from the beginning of this file, at which the next read or write occurs.

Usage

From source file:MainClass.java

public static void main(String[] argv) throws IOException {
    RandomAccessFile randomAccessFile = new RandomAccessFile("test.dat", "r");

    randomAccessFile.seek(1000);

    FileChannel fileChannel = randomAccessFile.getChannel();

    // This will print "1000"
    System.out.println("file pos: " + fileChannel.position());

    randomAccessFile.seek(500);/*from  ww  w.  ja v  a2 s  .c  o m*/

    // This will print "500"
    System.out.println("file pos: " + fileChannel.position());

    fileChannel.position(200);

    // This will print "200"
    System.out.println("file pos: " + randomAccessFile.getFilePointer());
}

From source file:InputOutputDemoPrimitive.java

public static void main(String[] a) throws Exception {

    //Read and write parts of file "raf.dat" in arbitrary order:
    RandomAccessFile raf = new RandomAccessFile("r.dat", "rw");
    raf.writeDouble(3.1415);// w w  w.ja  v  a 2  s. c  o m
    raf.writeInt(42);
    raf.seek(0);
    System.out.println(raf.readDouble() + " " + raf.readInt());

}

From source file:Main.java

public static void main(String[] args) {
    try {/*w  w w .  j a  v  a2s  . co  m*/
        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();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {//from   w  w w. j  a va 2  s.  c  o m
        int f = 1234;

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeInt(f);

        raf.seek(0);

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

        raf.seek(0);

        raf.writeInt(200);

        raf.seek(0);

        System.out.println(raf.readInt());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {//from   ww w. j  a  v  a  2s .com
        long f = 123456789909876L;

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeLong(f);

        raf.seek(0);

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

        raf.seek(0);

        raf.writeLong(20000000000l);

        raf.seek(0);

        System.out.println(raf.readLong());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {/* w  w w. j  av  a2 s .  c  o m*/
        int i = 70;

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeChar(i);

        raf.seek(0);

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

        raf.seek(0);

        raf.writeChar(71);

        raf.seek(0);

        System.out.println(raf.readChar());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {//from   w  w  w .j a  va  2s  .c o m
        boolean b = true;
        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeBoolean(false);

        raf.seek(0);

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

        raf.writeBoolean(b);

        raf.seek(1);

        System.out.println(raf.readBoolean());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {/*from  w  ww  . j  a va2 s  .c o m*/
        short s = 15;

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeShort(s);

        raf.seek(0);

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

        raf.seek(0);

        raf.writeShort(20);

        raf.seek(0);

        System.out.println(raf.readShort());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {//from  www . ja  va 2 s. c  om
        float f = 1234.5678f;

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeFloat(f);

        raf.seek(0);

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

        raf.seek(0);

        raf.writeFloat(123.4567f);

        raf.seek(0);

        System.out.println(raf.readFloat());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {//from   w w  w  .ja  v a2  s .  co  m
        double d = 1234.5678;

        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeDouble(d);

        raf.seek(0);

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

        raf.seek(0);

        raf.writeDouble(123.4567);

        raf.seek(0);

        System.out.println(raf.readDouble());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}