Java File Read via ByteBuffer readFromFile(String fileName)

Here you can find the source of readFromFile(String fileName)

Description

read From File

License

Open Source License

Declaration

public final static ByteBuffer readFromFile(String fileName) throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;

public class Main {
    public final static ByteBuffer readFromFile(String fileName) throws IOException {
        RandomAccessFile fis = null;
        MappedByteBuffer buf = null;
        try {//from   ww w .ja v a 2  s.  c  o m
            fis = new RandomAccessFile(fileName, "r");
            final FileChannel fc = fis.getChannel();
            int size = (int) fc.size();
            buf = fc.map(MapMode.READ_ONLY, 0, size);
            return buf;
        } finally {
            fis.close();
            //          closeDirectBuffer(buf);
        }
    }
}

Related

  1. readFlashString(DataInputStream s)
  2. readFloat(BufferedReader br)
  3. ReadFloat(InputStream is)
  4. readFromBuffer(byte[] buffer, int start, int end)
  5. readFromFile(Path path)
  6. readFromSocket(SocketChannel channel)
  7. readFull(Reader in)
  8. readFully(final Reader input, final char[] buffer, final int offset, final int length)
  9. readFully(InputStream in)