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

public static void main(String[] args) throws IOException {
    FileChannel c = new FileInputStream(args[0]).getChannel();
    OutputStreamWriter w = new OutputStreamWriter(System.out);
    Charset utf8 = Charset.forName("UTF-8");
    ChannelToWriter.copy(c, w, utf8);// w w  w.  j  a  v a  2  s.  c  om
    c.close();
    w.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();/*ww w. j  av a  2 s  . c  o m*/

    channel.close();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    FileInputStream fIn = new FileInputStream("test.txt");
    FileChannel fChan = fIn.getChannel();

    long fSize = fChan.size();
    ByteBuffer mBuf = ByteBuffer.allocate((int) fSize);

    fChan.read(mBuf);/*from ww w. j a v a  2 s  .  c  o m*/
    mBuf.rewind();

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

From source file:Main.java

public static void main(String args[]) throws Exception {
    FileInputStream fIn = new FileInputStream("test.txt");
    FileChannel fChan = fIn.getChannel();

    long fSize = fChan.size();
    ByteBuffer mBuf = ByteBuffer.allocate((int) fSize);

    fChan.read(mBuf, 10);/*from w w  w  .  j  a  va2s. co  m*/
    mBuf.rewind();

    for (int i = 0; i < fSize; i++) {
        System.out.print((char) mBuf.get());
    }
    fChan.close();
    fIn.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));/* ww  w .j a  v  a 2  s . c  om*/
        mbb.put(j, b);
    }
    fc.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 www  .  ja v a  2s  . c  om*/

    MappedByteBuffer mbb = buf.load();

    channel.close();
}

From source file:MappedChannelWrite.java

public static void main(String args[]) {
    RandomAccessFile fOut;/*w  w  w. jav a 2  s .  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:MainClass.java

public static void main(String[] args) throws Exception {
    String fromFileName = args[0];
    String toFileName = args[1];//w  ww  .  j av  a2  s .  c  o m
    FileChannel in = new FileInputStream(fromFileName).getChannel();
    FileChannel out = new FileOutputStream(toFileName).getChannel();

    ByteBuffer buff = ByteBuffer.allocate(32 * 1024);

    while (in.read(buff) > 0) {
        buff.flip();
        out.write(buff);
        buff.clear();
    }

    in.close();
    out.close();
}

From source file:CopyChannels.java

public static void main(String[] args) throws Exception {
    String fromFileName = "from.txt";
    String toFileName = "to.txt";
    FileChannel in = new FileInputStream(fromFileName).getChannel();
    FileChannel out = new FileOutputStream(toFileName).getChannel();

    ByteBuffer buff = ByteBuffer.allocate(32 * 1024);

    while (in.read(buff) > 0) {
        buff.flip();/*from   ww w .  j  a v a2 s  . c o m*/
        out.write(buff);
        buff.clear();
    }

    in.close();
    out.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String fromFileName = "from.txt";
    String toFileName = "to.txt";
    FileChannel in = new FileInputStream(fromFileName).getChannel();
    FileChannel out = new FileOutputStream(toFileName).getChannel();

    ByteBuffer buff = ByteBuffer.allocateDirect(32 * 1024);

    while (in.read(buff) > 0) {
        buff.flip();/*from   w  w  w . ja  va  2s.  com*/
        out.write(buff);
        buff.clear();
    }

    in.close();
    out.close();
}