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 Exception {
    String fromFileName = args[0];
    String toFileName = args[1];/*from ww w  .j av  a 2  s .c  o  m*/
    FileChannel in = new FileInputStream(fromFileName).getChannel();
    FileChannel out = new FileOutputStream(toFileName).getChannel();
    in.transferTo(0, (int) in.size(), out);
    in.close();
    out.close();
}

From source file:BufferToText.java

public static void main(String[] args) throws Exception {
    FileChannel fc = new FileOutputStream("data2.txt").getChannel();
    fc.write(ByteBuffer.wrap("Some text".getBytes()));
    fc.close();
    fc = new FileInputStream("data2.txt").getChannel();
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    fc.read(buff);/* w  w  w  .  j a  v  a 2s . c  om*/
    buff.flip();
    // Doesn't work:
    System.out.println(buff.asCharBuffer());
    // Decode using this system's default Charset:
    buff.rewind();
    String encoding = System.getProperty("file.encoding");
    System.out.println("Decoded using " + encoding + ": " + Charset.forName(encoding).decode(buff));
    // Or, we could encode with something that will print:
    fc = new FileOutputStream("data2.txt").getChannel();
    fc.write(ByteBuffer.wrap("Some text".getBytes("UTF-16BE")));
    fc.close();
    // Now try reading again:
    fc = new FileInputStream("data2.txt").getChannel();
    buff.clear();
    fc.read(buff);
    buff.flip();
    System.out.println(buff.asCharBuffer());
    // Use a CharBuffer to write through:
    fc = new FileOutputStream("data2.txt").getChannel();
    buff = ByteBuffer.allocate(24); // More than needed
    buff.asCharBuffer().put("Some text");
    fc.write(buff);
    fc.close();
    // Read and display:
    fc = new FileInputStream("data2.txt").getChannel();
    buff.clear();
    fc.read(buff);
    buff.flip();
    System.out.println(buff.asCharBuffer());
}

From source file:Main.java

public static void main(String args[]) throws IOException {
    FileInputStream fis = new FileInputStream("FileChannelExample.java");
    FileChannel fc = fis.getChannel();

    ByteBuffer bb = ByteBuffer.allocate((int) fc.size());

    fc.read(bb);//ww  w . jav  a  2s  .co m
    bb.flip();

    String fileContent = new String(bb.array());

    fc.close();
    fc = null;

    System.out.println("fileContent = " + fileContent);
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    FileChannel fc = new FileInputStream(new File("temp.tmp")).getChannel();
    IntBuffer ib = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()).asIntBuffer();
    while (ib.hasRemaining())
        ib.get();/*from w w w.j a v a2 s .  c o  m*/
    fc.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    FileChannel fc = new FileOutputStream("data2.txt").getChannel();
    ByteBuffer buff = ByteBuffer.allocate(24); // More than needed
    buff.asCharBuffer().put("Some text");
    fc.write(buff);/* w  w w  . j a  va  2s  .c  om*/
    fc.close();

    fc = new FileInputStream("data2.txt").getChannel();
    buff.clear();
    fc.read(buff);
    buff.flip();
    System.out.println(buff.asCharBuffer());

}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    FileChannel fc = new RandomAccessFile("temp.tmp", "rw").getChannel();
    IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
    for (int i = 0; i < 10; i++)
        ib.put(i);//from  w w w  . ja  va2  s  . c om
    fc.close();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    FileChannel srcChannel = new FileInputStream("srcFilename").getChannel();
    FileChannel dstChannel = new FileOutputStream("dstFilename").getChannel();

    dstChannel.transferFrom(srcChannel, 0, srcChannel.size());
    srcChannel.close();
    dstChannel.close();// w w  w .java  2 s  . c om
}

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

    buf.force();// w  w  w . ja va  2 s  .  c om

    channel.close();
}

From source file:MainClass.java

public static void main(String[] args) throws IOException {
    FileChannel fc = new RandomAccessFile(new File("temp.tmp"), "rw").getChannel();
    IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
    ib.put(0);/* w  w w . j  ava2s.  c om*/
    for (int i = 1; i < 10; i++)
        ib.put(ib.get(i - 1));
    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();

    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  .jav a2  s.  c o  m

    channel.close();
}