Example usage for java.io RandomAccessFile getChannel

List of usage examples for java.io RandomAccessFile getChannel

Introduction

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

Prototype

public final FileChannel getChannel() 

Source Link

Document

Returns the unique java.nio.channels.FileChannel FileChannel object associated with this file.

Usage

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[] 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));/*w  w  w . jav  a  2 s  .c  o m*/
        mbb.put(j, b);
    }
    fc.close();
}

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    RandomAccessFile raf = new RandomAccessFile("test.txt", "r");
    FileChannel fc = raf.getChannel();
    MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

    buffer.clear();/*from   w  w  w.j  a va 2s. c o m*/
    buffer.flip();

    System.out.println("hasArray=" + buffer.hasArray());
    System.out.println(buffer.toString());

    System.out.flush();
}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    String filename = "test.dat";

    RandomAccessFile raf1 = new RandomAccessFile(filename, "rw");
    FileChannel fc1 = raf1.getChannel();

    RandomAccessFile raf2 = new RandomAccessFile(filename, "rw");
    FileChannel fc2 = raf2.getChannel();

    System.out.println("Grabbing first lock");
    FileLock lock1 = fc1.lock(0L, Integer.MAX_VALUE, false);

    System.out.println("Grabbing second lock");
    FileLock lock2 = fc2.lock(5, 10, false);

    System.out.println("Exiting");
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {

    if (args.length < 2) {
        System.err.println("Usage: java MainClass inFile1 inFile2... outFile");
        return;/* w  ww  .  j a  va2 s . c o  m*/
    }

    ByteBuffer[] buffers = new ByteBuffer[args.length - 1];
    for (int i = 0; i < args.length - 1; i++) {
        RandomAccessFile raf = new RandomAccessFile(args[i], "r");
        FileChannel channel = raf.getChannel();
        buffers[i] = channel.map(FileChannel.MapMode.READ_ONLY, 0, raf.length());
    }

    FileOutputStream outFile = new FileOutputStream(args[args.length - 1]);
    FileChannel out = outFile.getChannel();
    out.write(buffers);
    out.close();
}

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 = null;/*w  ww .j  a  va2  s  . com*/
    try {
        lock = fileChannel.lock(0, 10, true);

    } catch (IOException e) {
        // Handle the exception
    } finally {
        if (lock != null) {
            try {
                lock.release();
            } catch (IOException e) {
                // Handle the exception
            }
        }
    }

}

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

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

public static void main(String[] argv) throws IOException {
    RandomAccessFile randomAccessFile = new RandomAccessFile("test.dat", "r");

    randomAccessFile.seek(1000);//from   w ww .  ja  v  a2  s  .  c  o  m

    FileChannel fileChannel = randomAccessFile.getChannel();

    // This will print "1000"
    System.out.println("file pos: " + fileChannel.position());

    randomAccessFile.seek(500);

    // This will print "500"
    System.out.println("file pos: " + fileChannel.position());

    fileChannel.position(200);

    // This will print "200"
    System.out.println("file pos: " + randomAccessFile.getFilePointer());
}

From source file:MappedChannelWrite.java

public static void main(String args[]) {
    RandomAccessFile fOut;
    FileChannel fChan;/*from www .j  a v  a 2  s  . c o m*/
    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

static public void main(String args[]) throws Exception {
    File infile = new File("inFilename");
    File outfile = new File("outFilename");

    RandomAccessFile inraf = new RandomAccessFile(infile, "r");
    RandomAccessFile outraf = new RandomAccessFile(outfile, "rw");

    FileChannel finc = inraf.getChannel();
    FileChannel foutc = outraf.getChannel();

    MappedByteBuffer inmbb = finc.map(FileChannel.MapMode.READ_ONLY, 0, (int) infile.length());

    Charset inCharset = Charset.forName("UTF8");
    Charset outCharset = Charset.forName("UTF16");

    CharsetDecoder inDecoder = inCharset.newDecoder();
    CharsetEncoder outEncoder = outCharset.newEncoder();

    CharBuffer cb = inDecoder.decode(inmbb);
    ByteBuffer outbb = outEncoder.encode(cb);

    foutc.write(outbb);/*from   w  w w  .  j a  v a2 s  .  c o  m*/

    inraf.close();
    outraf.close();
}