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(String name)
Reads an array of bytes from a file.
try (RandomAccessFile e = new RandomAccessFile(name, "r")) {
    MappedByteBuffer buf = e.getChannel().map(MapMode.READ_ONLY, 0L, e.length());
    byte[] data;
    if (buf.hasArray()) {
        data = buf.array();
        return data;
    byte[] array = new byte[buf.remaining()];
...
byte[]readFile(String name)
read File
try {
    RandomAccessFile raf = new RandomAccessFile(name, "r");
    ByteBuffer buf = raf.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, raf.length());
    try {
        if (buf.hasArray()) {
            return buf.array();
        } else {
            byte[] array = new byte[buf.remaining()];
...
StringreadFile(String path)
read File
return readFile(new File(path));
StringreadFile(String path)
Read file.
try {
    FileInputStream stream = null;
    try {
        stream = new FileInputStream(new File(path));
        FileChannel fc = stream.getChannel();
        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        return Charset.defaultCharset().decode(bb).toString();
    } catch (FileNotFoundException e) {
...
StringreadFile(String path)
Read File into a String.
FileInputStream stream = new FileInputStream(new File(path));
try {
    FileChannel fc = stream.getChannel();
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    return Charset.defaultCharset().decode(bb).toString();
} finally {
    stream.close();
StringreadFile(String path)
read File
FileInputStream stream = new FileInputStream(new File(path));
try {
    FileChannel fc = stream.getChannel();
    MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    return Charset.defaultCharset().decode(bb).toString();
} finally {
    stream.close();
byte[]readFileAsByteArray(File file)
read File As Byte Array
ByteBuffer bb;
FileChannel fc = null;
try {
    fc = new FileInputStream(file).getChannel();
    bb = ByteBuffer.allocate((int) fc.size());
    fc.read(bb);
} finally {
    if (fc != null)
...
byte[]readFileAsByteArray(String path)
This method reads file as binary data.
return internalReadFileAsByteArray(path).array();
String[]readFileAsStringArray(File file)
read File As String Array
return readFileAsString(file).split("\n");
voidreadFileChannelFully(FileChannel fileChannel, byte buf[], int off, int len)
Reads len bytes in a loop using the channel of the stream
int toRead = len;
ByteBuffer byteBuffer = ByteBuffer.wrap(buf, off, len);
while (toRead > 0) {
    int ret = fileChannel.read(byteBuffer);
    if (ret < 0) {
        throw new IOException("Premeture EOF from inputStream");
    toRead -= ret;
...