Example usage for java.io RandomAccessFile RandomAccessFile

List of usage examples for java.io RandomAccessFile RandomAccessFile

Introduction

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

Prototype

public RandomAccessFile(File file, String mode) throws FileNotFoundException 

Source Link

Document

Creates a random access file stream to read from, and optionally to write to, the file specified by the File argument.

Usage

From source file:Main.java

public static void main(String[] args) {
    try {//from   w ww  .j  av a  2  s .co  m
        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:MainClass.java

public static void main(String args[]) {

    try {//from  w ww.  j  av  a  2  s.c o m

        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 {// www  .j a va2  s  .c o m
        short s = 15000;

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

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

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

        // print the short
        System.out.println(raf.readShort());

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

        // write something in the file
        raf.writeShort(134);

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

        // print the short
        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   ww  w. j  a va  2 s. co m
        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:Main.java

public static void main(String[] args) {
    try {//  w  w  w  . ja  v  a2s .c o  m
        float f = 1234.56f;

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

        // write something in the file
        raf.writeFloat(987.654f);

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

        // read float
        System.out.println(raf.readFloat());

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

        // write a float
        raf.writeFloat(f);

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

        // read float
        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 ww.ja v  a 2 s . com
        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();
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    RandomAccessFile raf = new RandomAccessFile("test.txt", "rw");
    FileChannel fileChannel = raf.getChannel();

    FileLock lock = fileChannel.lock();

}

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());/*  w ww .  j  ava  2  s . c  o m*/

    // Append to the end
    raf.writeChars("aString");
    raf.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    RandomAccessFile f = new RandomAccessFile("hello.txt", "rw");
    FileChannel fc = f.getChannel();
    MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, 0, f.length());

    int len = (int) f.length();
    for (int i = 0, j = len - 1; i < j; i++, j--) {
        byte b = mbb.get(i);
        mbb.put(i, mbb.get(j));//from ww  w.jav  a  2  s  .c  o m
        mbb.put(j, b);
    }
    fc.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    FileChannel rwChannel = new RandomAccessFile(new File("test.txt"), "rw").getChannel();
    ByteBuffer wrBuf = rwChannel.map(FileChannel.MapMode.READ_WRITE, 0, (int) rwChannel.size());
}