Example usage for java.nio.channels FileChannel read

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

Introduction

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

Prototype

public final long read(ByteBuffer[] dsts) throws IOException 

Source Link

Document

Reads a sequence of bytes from this channel into the given buffers.

Usage

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);
    bb.flip();/* www. java 2s.c  o m*/

    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 Exception {
    File aFile = new File("charData.xml");
    FileInputStream inFile = null;

    inFile = new FileInputStream(aFile);

    FileChannel inChannel = inFile.getChannel();
    ByteBuffer buf = ByteBuffer.allocate(48);

    while (inChannel.read(buf) != -1) {
        System.out.println("String read: " + ((ByteBuffer) (buf.flip())).asCharBuffer().get(0));
        buf.clear();/*  w  w  w  .  j  a  v  a 2s. c  om*/
    }
    inFile.close();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileChannel fc = new FileInputStream("data.txt").getChannel();
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    fc.read(buff);
    buff.flip();//from  w  ww.  j  a v  a2  s  .  c  o  m
    while (buff.hasRemaining())
        System.out.print((char) buff.get());
}

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);
    mBuf.rewind();// ww w  .  j a va  2  s . co m

    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 {
    File aFile = new File("main.java");
    FileInputStream inFile = new FileInputStream(aFile);
    FileChannel inChannel = inFile.getChannel();
    ByteBuffer lengthBuf = ByteBuffer.allocate(8);
    while (true) {
        if (inChannel.read(lengthBuf) == -1) {
            break;
        }// www.j  a v a2  s.  c o m
        lengthBuf.flip();
        int strLength = (int) lengthBuf.getDouble();
        ByteBuffer buf = ByteBuffer.allocate(2 * strLength + 8);
        if (inChannel.read(buf) == -1) {
            break;
        }
        buf.flip();
        byte[] strChars = new byte[2 * strLength];
        buf.get(strChars);
        System.out.println(strLength);
        System.out.println(ByteBuffer.wrap(strChars).asCharBuffer());
        System.out.println(buf.getLong());
        lengthBuf.clear();
    }
    inFile.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();

    for (ByteBuffer buffer = ByteBuffer.allocate(1024 * 1024); inChannel.read(buffer) != -1; buffer.clear()) {
        buffer.flip();//from   ww w.j a  v  a2 s .c  om
        while (buffer.hasRemaining())
            outChannel.write(buffer);
    }

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

From source file:Main.java

static public void main(String args[]) throws Exception {
    FileInputStream fin = new FileInputStream("infile.txt");
    FileOutputStream fout = new FileOutputStream("outfile.txt");

    FileChannel inc = fin.getChannel();
    FileChannel outc = fout.getChannel();

    ByteBuffer bb = ByteBuffer.allocate(1024);

    while (true) {
        int ret = inc.read(bb);
        if (ret == -1)
            break;
        bb.flip();// ww  w .  j a  va2s .  c  o m
        outc.write(bb);
        bb.clear();
    }
}

From source file:Main.java

static public void main(String args[]) throws Exception {
    FileInputStream fin = new FileInputStream("infile.txt");
    FileOutputStream fout = new FileOutputStream("outfile.txt");

    FileChannel inc = fin.getChannel();
    FileChannel outc = fout.getChannel();

    ByteBuffer bb = ByteBuffer.allocateDirect(1024);

    while (true) {
        int ret = inc.read(bb);
        if (ret == -1)
            break;
        bb.flip();/*from   w w  w.ja va  2 s  .c  o  m*/
        outc.write(bb);
        bb.clear();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    String source = "s.txt";
    String destination = "d.txt";

    FileInputStream fis = new FileInputStream(source);
    FileOutputStream fos = new FileOutputStream(destination);

    FileChannel fci = fis.getChannel();
    FileChannel fco = fos.getChannel();

    ByteBuffer buffer = ByteBuffer.allocate(1024);

    while (true) {
        int read = fci.read(buffer);

        if (read == -1)
            break;
        buffer.flip();/*from   ww w . j  a  va  2s.  c o  m*/
        fco.write(buffer);
        buffer.clear();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    FileChannel in = new FileInputStream("source.txt").getChannel(),
            out = new FileOutputStream("target.txt").getChannel();
    ByteBuffer buffer = ByteBuffer.allocate(BSIZE);
    while (in.read(buffer) != -1) {
        buffer.flip();/* w  w  w . j  a v  a 2s  .c  o  m*/
        out.write(buffer);
        buffer.clear();
    }
}