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

public static void main(String args[]) throws Exception {
    RandomAccessFile raf = new RandomAccessFile(args[0], "r");
    long count = 10;
    long position = raf.length();
    position -= count;//www. j av a2s . co m
    if (position < 0)
        position = 0;
    raf.seek(position);
    while (true) {
        try {
            byte b = raf.readByte();
            System.out.print((char) b);
        } catch (EOFException eofe) {
            break;
        }
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {/*from ww  w  .  ja  v  a  2s . c  o m*/
        byte[] b = { 1, 2, 3, 4, 5, 6 };

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

        // write 2 bytes in the file
        raf.write(b, 2, 2);

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

        // print the two bytes we wrote
        System.out.println(raf.readByte());
        System.out.println(raf.readByte());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    RandomAccessFile rf = new RandomAccessFile("test.dat", "rw");
    for (int i = 0; i < 10; i++)
        rf.writeDouble(i * 1.414);/*from w  w  w  . java 2  s. com*/
    rf.close();
    rf = new RandomAccessFile("test.dat", "rw");
    rf.seek(5 * 8);
    rf.writeDouble(47.0001);
    rf.close();
    rf = new RandomAccessFile("test.dat", "r");
    for (int i = 0; i < 10; i++)
        System.out.println("Value " + i + ": " + rf.readDouble());
    rf.close();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    RandomAccessFile file = new RandomAccessFile("scores.html", "rw");
    for (int i = 1; i <= 6; i++) {
        System.out.println(file.readLine());
    }/*from  ww w .j a v  a  2s. c o  m*/
    long current = file.getFilePointer();
    file.seek(current + 6);
    file.write("34".getBytes());
    for (int i = 1; i <= 5; i++) {
        System.out.println(file.readLine());
    }
    current = file.getFilePointer();
    file.seek(current + 6);
    file.write("27".getBytes());
    file.close();
}

From source file:Main.java

public static void main(String[] args) {
    try {/*w  w  w.ja  v a2  s  . c o  m*/

        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 0 position
        raf.seek(0);

        // attempt to skip 10 bytes
        System.out.println(raf.skipBytes(10));

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

        // set the file pointer to position 8
        raf.seek(8);

        // attempt to skip 10 more bytes
        System.out.println(raf.skipBytes(10));
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {/*from  w  ww. ja v a  2 s. c om*/
        RandomAccessFile raf = new RandomAccessFile("c:\\temp\\RAFsample.txt", "rw");
        raf.writeInt(10);
        raf.writeInt(43);
        raf.writeInt(88);
        raf.writeInt(455);

        raf.seek((3 - 1) * 4);
        raf.writeInt(99);
        raf.seek(0);
        int i = raf.readInt();
        while (i != -1) {
            System.out.println(i);

            i = raf.readInt();
        }
        raf.close();
    } catch (IOException e) {
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {/*  w w w.  j av a  2  s.  c om*/
        long l = 12345676789098L;

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

        // write something in the file
        raf.writeLong(l);

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

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

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

        // write something in the file
        raf.writeLong(12345676789099L);

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

}

From source file:Main.java

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

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

        // write something in the file
        raf.writeUTF(s);

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

        // create an array equal to the length of raf
        byte[] arr = new byte[(int) raf.length()];

        // read the file
        raf.readFully(arr);

        // create a new string based on arr
        String s2 = new String(arr);

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

}

From source file:Main.java

public static void main(String[] args) {
    try {//from   www .j  a  va  2 s  .  c om
        String s = "Hello world from java2s.com";

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

        // write something in the file
        raf.writeUTF(s);

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

        // create an array equal to the length of raf
        byte[] arr = new byte[10];

        // read the file
        raf.readFully(arr, 3, 7);

        // create a new string based on arr
        String s2 = new String(arr);

        // print it
        System.out.println(s2);
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {//from  w  ww.  ja v  a 2 s.  c  o  m
        char c = 'J';

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

        // write something in the file
        raf.writeChar('A');

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

        // read char
        System.out.println(raf.readChar());

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

        // write a char at the start
        raf.writeChar(c);

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

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

}