Example usage for java.nio.channels FileChannel close

List of usage examples for java.nio.channels FileChannel close

Introduction

In this page you can find the example usage for java.nio.channels FileChannel close.

Prototype

public final void close() throws IOException 

Source Link

Document

Closes this channel.

Usage

From source file:MainClass.java

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

    FileInputStream inFile = new FileInputStream(args[0]);
    FileOutputStream outFile = new FileOutputStream(args[1]);

    FileChannel inChannel = inFile.getChannel();
    FileChannel outChannel = outFile.getChannel();

    for (ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024); inChannel.read(buffer) != -1; buffer.clear()) {
        buffer.flip();//from  w w  w.  ja  v  a  2  s  .  c o m
        while (buffer.hasRemaining())
            outChannel.write(buffer);
    }

    inChannel.close();
    outChannel.close();
}

From source file:MainClass.java

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

    FileInputStream inFile = new FileInputStream(args[0]);
    FileOutputStream outFile = new FileOutputStream(args[1]);

    FileChannel inChannel = inFile.getChannel();
    FileChannel outChannel = outFile.getChannel();

    FileLock outLock = outChannel.lock();
    FileLock inLock = inChannel.lock(0, inChannel.size(), true);

    inChannel.transferTo(0, inChannel.size(), outChannel);

    outLock.release();/*from   w  ww. ja  v  a 2 s. com*/
    inLock.release();

    inChannel.close();
    outChannel.close();
}

From source file:ExplicitChannelWrite.java

public static void main(String args[]) {
    FileOutputStream fOut;//from   w w  w  .  j a  v  a  2s .  c  o  m
    FileChannel fChan;
    ByteBuffer mBuf;

    try {
        fOut = new FileOutputStream("test.txt");
        fChan = fOut.getChannel();
        mBuf = ByteBuffer.allocateDirect(26);
        for (int i = 0; i < 26; i++)
            mBuf.put((byte) ('A' + i));
        mBuf.rewind();
        fChan.write(mBuf);
        fChan.close();
        fOut.close();
    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    RandomAccessFile randomAccessFile;
    FileChannel fileChannel;
    ByteBuffer byteBuffer;/*from  w w w.j a v a 2s.com*/

    try {
        randomAccessFile = new RandomAccessFile("test.txt", "rw");
        fileChannel = randomAccessFile.getChannel();
        byteBuffer = fileChannel.map(FileChannel.MapMode.READ_WRITE, 0, 26);
        for (int i = 0; i < 10; i++)
            byteBuffer.put((byte) ('A' + i));
        fileChannel.close();
        randomAccessFile.close();
    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}

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();

    for (int i = 0; i < PRIMESREQUIRED; i++) {
        index = 8 * (int) (PRIMECOUNT * Math.random());
        primes[i] = buf.getLong(index);/*ww w . ja  v a  2s.c  o  m*/
        buf.putLong(index, REPLACEMENT);
    }

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

From source file:MappedChannelRead.java

public static void main(String args[]) {
    FileInputStream fIn;/*from w  w  w.j ava2s.  c om*/
    FileChannel fChan;
    long fSize;
    MappedByteBuffer mBuf;

    try {
        fIn = new FileInputStream("test.txt");
        fChan = fIn.getChannel();
        fSize = fChan.size();
        mBuf = fChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);
        for (int i = 0; i < fSize; i++)
            System.out.print((char) mBuf.get());

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

From source file:NIOCopy.java

public static void main(String args[]) throws Exception {
    FileInputStream fIn;/*from w  w  w  .j a  v  a 2 s . c om*/
    FileOutputStream fOut;
    FileChannel fIChan, fOChan;
    long fSize;
    MappedByteBuffer mBuf;

    fIn = new FileInputStream(args[0]);
    fOut = new FileOutputStream(args[1]);

    fIChan = fIn.getChannel();
    fOChan = fOut.getChannel();
    fSize = fIChan.size();

    mBuf = fIChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);
    fOChan.write(mBuf); // this copies the file
    fIChan.close();
    fIn.close();

    fOChan.close();
    fOut.close();
}

From source file:ExplicitChannelRead.java

public static void main(String args[]) {
    FileInputStream fIn;// w w w. j a v a  2s . co  m
    FileChannel fChan;
    long fSize;
    ByteBuffer mBuf;

    try {
        fIn = new FileInputStream("test.txt");
        fChan = fIn.getChannel();
        fSize = fChan.size();
        mBuf = ByteBuffer.allocate((int) fSize);
        fChan.read(mBuf);
        mBuf.rewind();
        for (int i = 0; i < fSize; i++)
            System.out.print((char) mBuf.get());
        fChan.close();
        fIn.close();
    } catch (IOException exc) {
        System.out.println(exc);
        System.exit(1);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    FileOutputStream fileOutputStream;
    FileChannel fileChannel;
    ByteBuffer byteBuffer;/*from  w  w  w. ja  v  a 2s. co m*/

    try {
        fileOutputStream = new FileOutputStream("test.txt");
        fileChannel = fileOutputStream.getChannel();
        byteBuffer = ByteBuffer.allocateDirect(26);

        for (int i = 0; i < 26; i++)
            byteBuffer.put((byte) ('A' + i));
        byteBuffer.rewind();
        fileChannel.write(byteBuffer);
        fileChannel.close();
        fileOutputStream.close();
    } catch (IOException exc) {
        System.out.println(exc);
    }
}

From source file:MainClass.java

public static void main(String args[]) {
    FileInputStream fIn;/*from w w  w  .  j  av a 2s . c  o m*/
    FileChannel fChan;
    long fSize;
    ByteBuffer mBuf;

    try {
        fIn = new FileInputStream("test.txt");

        fChan = fIn.getChannel();

        fSize = fChan.size();

        mBuf = ByteBuffer.allocate((int) fSize);

        fChan.read(mBuf);

        mBuf.rewind();

        for (int i = 0; i < fSize; i++)
            System.out.print((char) mBuf.get());
        fChan.close();
        fIn.close();
    } catch (IOException exc) {
        System.out.println(exc);
    }
}