Java Utililty Methods File Read via ByteBuffer

List of utility methods to do File Read via ByteBuffer

Description

The list of methods to do File Read via ByteBuffer are organized into topic(s).

Method

byte[]readFile(File file)
read File
ByteArrayOutputStream output = new ByteArrayOutputStream(4096);
BufferedInputStream input = null;
try {
    input = new BufferedInputStream(new FileInputStream(file));
    byte[] buffer = new byte[2048];
    for (;;) {
        int len = input.read(buffer);
        if (len == (-1))
...
StringreadFile(File file)
read File
FileInputStream inputStream = new FileInputStream(file);
FileChannel channel = inputStream.getChannel();
ByteBuffer buffer = ByteBuffer.allocate((int) channel.size());
channel.read(buffer);
channel.close();
inputStream.close();
return new String(buffer.array());
byte[]readFile(File file)
read File
RandomAccessFile raf = new RandomAccessFile(file, "r");
byte[] res = readStream(raf.getChannel());
raf.close();
return res;
ByteBufferreadFile(File file)
Reads the contents of the given File into a ByteBuffer.
DataInputStream dataInputStream = null;
try {
    int byteCount = (int) file.length();
    FileInputStream fileInputStream = new FileInputStream(file);
    dataInputStream = new DataInputStream(fileInputStream);
    final byte[] bytes = new byte[byteCount];
    dataInputStream.readFully(bytes);
    return ByteBuffer.wrap(bytes);
...
java.nio.ByteBufferreadFile(java.io.File file)
read File
java.io.FileInputStream fis = new java.io.FileInputStream(file);
java.nio.ByteBuffer buffer = java.nio.ByteBuffer.allocate(PAGE_SIZE);
java.nio.channels.ReadableByteChannel channel = java.nio.channels.Channels.newChannel(fis);
int count = 0;
while (count >= 0) {
    count = channel.read(buffer);
    if (count > 0 && !buffer.hasRemaining()) {
        java.nio.ByteBuffer biggerBuffer = java.nio.ByteBuffer.allocate(buffer.limit() + PAGE_SIZE);
...
StringreadFile(String filename)
read File
if (filename != null) {
    byte[] bytes = getBytes(filename);
    return new String(bytes);
return null;
StringreadFile(String filename)
read File
return readFile(new File(filename));
ByteBufferreadFile(String fileName)
read File
ByteBuffer buf = null;
FileChannel fc = null;
try {
    FileInputStream fis = new FileInputStream(fileName);
    fc = fis.getChannel();
    int size = (int) fc.size();
    buf = readFileFragment(fc, 0, size);
} catch (FileNotFoundException ex) {
...
StringreadFile(String filename)
read File
return readFile(filename, false);
StringreadFile(String filePath)
Read a file using nio
String readString = "";
RandomAccessFile file = new RandomAccessFile(filePath, "r");
FileChannel channel = file.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
while (channel.read(buffer) > 0) {
    buffer.flip();
    for (int i = 0; i < buffer.limit(); i++)
        readString += (char) buffer.get();
...