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: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);/*  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 {
    FileInputStream fIn = new FileInputStream("test.txt");
    FileChannel fChan = fIn.getChannel();

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

    fChan.read(mBuf);//from   w w 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:MainClass.java

public static void main(String[] args) throws Exception {
    FileChannel fc = new RandomAccessFile("data.txt", "rw").getChannel();
    fc.position(fc.size());
    fc.write(ByteBuffer.wrap("Some more".getBytes()));
    fc.close();//from   w  ww.  ja  va2  s .c om

}

From source file:Main.java

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

    long startRegion = 0;
    long endRegion = fc.size();
    MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_ONLY, startRegion, endRegion);
    while (mbb.hasRemaining()) {
        System.out.print((char) mbb.get());
    }// w  ww. j  a  va  2 s.  c  om
    fis.close();
}

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);//from www .  j  a v a 2  s . co m
    bb.flip();

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

    fc.close();
    fc = null;

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

From source file:BufferConverter.java

public static void main(String[] arguments) {
    try {//from  w w  w.j  av  a2 s.  com
        String data = "friends.dat";
        FileInputStream inData = new FileInputStream(data);
        FileChannel inChannel = inData.getChannel();
        long inSize = inChannel.size();
        ByteBuffer source = ByteBuffer.allocate((int) inSize);
        inChannel.read(source, 0);
        source.position(0);
        for (int i = 0; source.remaining() > 0; i++)
            System.out.print(source.get() + " ");

        source.position(0);
        Charset ascii = Charset.forName("US-ASCII");
        CharsetDecoder toAscii = ascii.newDecoder();
        CharBuffer destination = toAscii.decode(source);
        destination.position(0);
        System.out.println("\n\nNew character data:");
        for (int i = 0; destination.remaining() > 0; i++)
            System.out.print(destination.get());
    } catch (Exception ioe) {
        System.out.println(ioe.getMessage());
    }
}

From source file:MainClass.java

public static void main(String[] argv) throws Exception {
    RandomAccessFile raf = new RandomAccessFile("test.txt", "r");
    FileChannel fc = raf.getChannel();
    MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

    buffer.clear();/*  ww  w .  j  a  va2  s .  c  o m*/
    buffer.flip();

    System.out.println("hasArray=" + buffer.hasArray());
    System.out.println(buffer.toString());

    System.out.flush();
}

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);/*  w  ww  .  j a v a  2s  . c om*/
    fc.close();

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    Pattern pattern = Pattern.compile("pattern");
    FileInputStream input = new FileInputStream("file.txt");
    FileChannel channel = input.getChannel();

    ByteBuffer bbuf = channel.map(FileChannel.MapMode.READ_ONLY, 0, (int) channel.size());
    CharBuffer cbuf = Charset.forName("8859_1").newDecoder().decode(bbuf);

    Matcher matcher = pattern.matcher(cbuf);
    while (matcher.find()) {
        String match = matcher.group();
        System.out.println(match);
    }//  ww w  .jav  a2 s. c  o  m
}

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  ww w  .ja  v  a  2s.com
    fc.close();
}