Example usage for java.nio.channels FileChannel size

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

Introduction

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

Prototype

public abstract long size() throws IOException;

Source Link

Document

Returns the current size of this channel's file.

Usage

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    File fromFile = new File("fromFile.txt");
    File toFile = new File("toFile.txt");
    FileInputStream inFile = new FileInputStream(fromFile);
    FileOutputStream outFile = new FileOutputStream(toFile);
    FileChannel inChannel = inFile.getChannel();
    FileChannel outChannel = outFile.getChannel();
    int bytesWritten = 0;
    long byteCount = inChannel.size();
    while (bytesWritten < byteCount) {
        bytesWritten += inChannel.transferTo(bytesWritten, byteCount - bytesWritten, outChannel);
    }//  w ww  . j  a v  a2s. co m
    inFile.close();
    outFile.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();
    in.transferTo(0, (int) in.size(), out);
    in.close();/*from  w ww  .  j  a v  a 2 s  .  c  o  m*/
    out.close();
}

From source file:MainClass.java

public static void main(String[] args) throws Exception {
    String fromFileName = args[0];
    String toFileName = args[1];//from w  ww .  j a  va2  s .co 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:Main.java

public static void main(String[] argv) throws Exception {
    File file = new File("filename");
    FileChannel roChannel = new RandomAccessFile(file, "r").getChannel();
    ByteBuffer readonlybuffer = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) roChannel.size());

    FileChannel rwChannel = new RandomAccessFile(file, "rw").getChannel();
    ByteBuffer writeonlybuffer = rwChannel.map(FileChannel.MapMode.READ_WRITE, 0, (int) rwChannel.size());

    // Create a private (copy-on-write) memory-mapped file.
    FileChannel pvChannel = new RandomAccessFile(file, "rw").getChannel();

    ByteBuffer privatebuffer = roChannel.map(FileChannel.MapMode.READ_WRITE, 0, (int) rwChannel.size());

}

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();//from  ww w . ja  va2 s  . c  om
    dstChannel.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();/*w  w w.  j a va  2  s  .com*/
    inLock.release();

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

From source file:ExplicitChannelRead.java

public static void main(String args[]) {
    FileInputStream fIn;/*from  w ww  . jav a2 s  .  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:MappedChannelRead.java

public static void main(String args[]) {
    FileInputStream fIn;//  w ww .jav a  2 s.  co  m
    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:MainClass.java

public static void main(String args[]) {
    FileInputStream fIn;//from ww  w.  j a va2  s . 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);
    }
}

From source file:Main.java

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

    FileChannel sourceChannel = new FileInputStream("sourceFile").getChannel();
    FileChannel sinkChannel = new FileOutputStream("newFile").getChannel();

    // Copy source file contents to the sink file
    sourceChannel.transferTo(0, sourceChannel.size(), sinkChannel);
}