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[] argv) throws Exception {
    File file = new File("filename");
    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
    MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, (int) channel.size());

    buf.put(0, (byte) 0xFF);

    System.out.println(buf.isLoaded());

    buf.force();// w w  w  . j  a  va2 s. co m

    channel.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("filename");
    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();

    FileLock lock = channel.lock(0, Long.MAX_VALUE, true);

    lock = channel.tryLock(0, Long.MAX_VALUE, true);

    boolean isShared = lock.isShared();

    lock.release();/*  w  w  w  .j av  a2  s.  co m*/

    channel.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("filename");
    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
    MappedByteBuffer buf = channel.map(FileChannel.MapMode.READ_WRITE, 0, (int) channel.size());

    buf.put(0, (byte) 0xFF);

    System.out.println(buf.isLoaded());

    buf.force();/*from w  ww .  ja v  a  2s.co m*/

    MappedByteBuffer mbb = buf.load();

    channel.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    FileChannel fc = new RandomAccessFile("data.txt", "rw").getChannel();
    fc.position(fc.size());//from   ww  w  . ja  v  a2s .c  om
    fc.write(ByteBuffer.wrap("Some more".getBytes()));
    fc.close();

}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    RandomAccessFile randomAccessFile = null;

    String line1 = "line\n";
    String line2 = "asdf1234\n";

    // read / write permissions
    randomAccessFile = new RandomAccessFile("yourFile.dat", "rw");

    randomAccessFile.writeBytes(line1);/*from   w w  w  . j  a v  a2 s. c o m*/
    randomAccessFile.writeBytes(line2);

    // Place the file pointer at the end of the first line
    randomAccessFile.seek(line1.length());

    byte[] buffer = new byte[line2.length()];
    randomAccessFile.read(buffer);
    System.out.println(new String(buffer));

    randomAccessFile.close();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("filename");
    // Create a read-only memory-mapped file
    FileChannel roChannel = new RandomAccessFile(file, "r").getChannel();

    ByteBuffer readonlybuffer = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) roChannel.size());

}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    File aFile = new File("C:/test.bin");
    RandomAccessFile ioFile = new RandomAccessFile(aFile, " rw");

    FileChannel ioChannel = ioFile.getChannel();
    final int PRIMESREQUIRED = 10;
    long[] primes = new long[PRIMESREQUIRED];

    int index = 0;
    final long REPLACEMENT = 999999L;

    final int PRIMECOUNT = (int) ioChannel.size() / 8;
    MappedByteBuffer buf = ioChannel.map(FileChannel.MapMode.READ_WRITE, 0L, ioChannel.size()).load();
    ioChannel.close();//w w w . j  a v a2  s .  c  om

    for (int i = 0; i < PRIMESREQUIRED; i++) {
        index = 8 * (int) (PRIMECOUNT * Math.random());
        primes[i] = buf.getLong(index);
        buf.putLong(index, REPLACEMENT);
    }

    for (long prime : primes) {
        System.out.printf("%12d", prime);
    }
    ioFile.close();
}

From source file:Main.java

License:asdf

public static void main(String[] args) throws Exception {
    RandomAccessFile randomAccessFile = null;

    String line1 = "java2s.com\n";
    String line2 = "asdf1234\n";

    // read / write permissions
    randomAccessFile = new RandomAccessFile("yourFile.dat", "rw");

    randomAccessFile.writeBytes(line1);//  w w w. ja va2s .co m
    randomAccessFile.writeBytes(line2);

    // Place the file pointer at the end of the first line
    randomAccessFile.seek(line1.length());

    byte[] buffer = new byte[line2.length()];
    randomAccessFile.read(buffer);
    System.out.println(new String(buffer));

    randomAccessFile.close();
}

From source file:MappedChannelWrite.java

public static void main(String args[]) {
    RandomAccessFile fOut;//from   w w w. j  a v a2s . c  om
    FileChannel fChan;
    ByteBuffer mBuf;

    try {
        fOut = new RandomAccessFile("test.txt", "rw");
        fChan = fOut.getChannel();
        mBuf = fChan.map(FileChannel.MapMode.READ_WRITE, 0, 26);
        for (int i = 0; i < 26; i++)
            mBuf.put((byte) ('A' + i));

        fChan.close();
        fOut.close();
    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    // Create a read/writeable file channel
    File file = new File("filename");
    FileChannel channel = new RandomAccessFile(file, "rw").getChannel();

    OutputStream os = Channels.newOutputStream(channel);
    os.close();/*from  w w w  . j av a2  s .  co  m*/
}