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:Main.java

public static void main(String[] args) {
    try {//  w  w w  .  ja v  a2s .co m
        byte[] b1 = { 15, 8 };

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

        raf.writeByte(b1[0]);

        raf.seek(0);

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

        raf.seek(0);

        raf.writeByte(b1[1]);

        raf.seek(0);

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

}

From source file:Main.java

public static void main(String[] args) {
    try {//ww w .j a  v a  2  s .  co m
        String s = "Hello World from java2s.com";

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

        raf.writeChars(s);

        raf.seek(0);

        for (int i = 0; i < 15; i++) {
            System.out.println(raf.readChar());
        }

        raf.seek(0);

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

        raf.seek(0);

        for (int i = 0; i < 20; i++) {
            System.out.println(raf.readChar());
        }
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    RandomAccessFile raf = new RandomAccessFile(new File("temp.tmp"), "rw");
    raf.writeInt(1);//from  w  w  w  .  j a  va2  s. c  om
    for (int i = 0; i < 10; i++) {
        raf.seek(raf.length() - 4);
        raf.writeInt(raf.readInt());
    }
    raf.close();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    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();/*from  w  w w.j  a va  2  s .com*/
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from  ww  w . jav  a 2  s .  co  m*/
        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeBytes("Hello World from java2s.com");

        raf.seek(0);

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

        raf.seek(0);

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

        raf.seek(0);

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

}

From source file:ReverseFile.java

public static void main(String args[]) throws Exception {
    RandomAccessFile raf = new RandomAccessFile(args[0], "r");
    long position = raf.length();
    while (position > 0) {
        position -= 1;/*from ww  w.  j ava  2  s  .co  m*/
        raf.seek(position);
        byte b = raf.readByte();
        System.out.print((char) b);
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {//from  ww  w  .  ja  va 2  s. com
        byte[] b = { 1 };

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

        // set the file pointer at 0 position
        raf.seek(0);

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

}

From source file:Main.java

public static void main(String[] args) throws IOException {
    RandomAccessFile raf = new RandomAccessFile("employee.dat", "rw");

    raf.writeUTF("J");
    raf.writeUTF("S");
    raf.writeDouble(4.0);/*  w w  w.  j  a  va 2s . c  o m*/
    raf.seek(0L);
    String fname = raf.readUTF();
    String lname = raf.readUTF();
    double salary = raf.readDouble();
    System.out.println("First name = " + fname);
    System.out.println("Last name = " + lname);
    System.out.println("Salary = " + salary);
    raf.close();
}

From source file:Main.java

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

    RandomAccessFile raf = new RandomAccessFile("a.dat", "rw");
    int x, y;//from   w w w  . ja va 2 s. c o  m

    for (long i = 0, j = raf.length() - 1; i < j; i++, j--) {
        raf.seek(i);
        x = raf.read();
        raf.seek(j);
        y = raf.read();

        raf.seek(j);
        raf.write(x);
        raf.seek(i);
        raf.write(y);

    }
    raf.close();
}

From source file:Main.java

public static void main(String[] args) {
    try {//from   w  w  w.  j  a  v a 2 s.co m
        int b1 = 15;
        int b2 = 20;

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

        // set the file pointer at 0 position
        raf.seek(0);

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

        raf.write(b2);

        raf.seek(1);

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

}