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 {//ww w . java  2s  .  c  o  m

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

        raf.writeUTF("java2s.com");

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

        // read and print the contents of the file
        System.out.println(raf.readUTF());

        // close the strea and release resources
        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  .c  o m
        RandomAccessFile raf = new RandomAccessFile("c:/test.txt", "rw");

        raf.writeUTF("java2s.com Hello World");

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

        // read and print the contents of the file
        System.out.println(raf.readUTF());

        // return the channel of the file
        System.out.println(raf.getChannel());

        // close the strea and release resources
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }
}

From source file:Main.java

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

        raf.writeUTF("java2s.com Hello World");

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

        // read and print the contents of the file
        System.out.println(raf.readUTF());

        // return the file descriptor of the stream
        System.out.println(raf.getFD());

        // close the strea and release resources
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:RandomIOApp.java

public static void main(String args[]) throws IOException {
    RandomAccessFile file = new RandomAccessFile("test.txt", "rw");
    file.writeBoolean(true);//w ww.  ja va 2  s. co  m
    file.writeInt(123456);
    file.writeChar('j');
    file.writeDouble(1234.56);
    file.seek(1);
    System.out.println(file.readInt());
    System.out.println(file.readChar());
    System.out.println(file.readDouble());
    file.seek(0);
    System.out.println(file.readBoolean());
    file.close();
}

From source file:Main.java

public static void main(String[] args) {
    try {//from  w  w  w .  ja v a  2  s . co 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 short
        System.out.println(raf.readUnsignedShort());

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

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

}

From source file:Main.java

public static void main(String[] args) {
    try {//from   w  ww .jav a  2  s  . c om
        double d = 1.2345678;

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

        raf.writeDouble(123.456789);

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

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

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

        // write a double at the start
        raf.writeDouble(d);

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

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

}

From source file:MainClass.java

public static void main(String args[]) {

    try {/*from w  w  w .  j  a va2 s. c om*/

        RandomAccessFile raf = new RandomAccessFile(args[0], "r");

        long position = raf.length();

        while (position > 0) {

            position -= 1;

            raf.seek(position);
            byte b = raf.readByte();

            System.out.print((char) b);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) {
    try {//w w w  .  j  a v  a  2s .c om
        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());

        // print current length
        System.out.println(raf.length());

        // set the file length to 30
        raf.setLength(30);

        System.out.println(raf.length());
        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 v a  2  s .co  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);

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

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

        raf.writeUTF("This is from java2s.com");

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

        // print the string
        System.out.println(raf.readUTF());
        raf.close();
    } catch (IOException ex) {
        ex.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) {
    try {//ww w .  jav a 2  s  . co  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 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();
    }

}