Java File Read via ByteBuffer readFile(File file)

Here you can find the source of readFile(File file)

Description

read File

License

Mozilla Public License

Declaration

public static ByteBuffer readFile(File file) throws IOException 

Method Source Code


//package com.java2s;
/*//w w w .  ja  v a 2  s  . co m
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

import java.io.File;
import java.io.FileInputStream;

import java.io.IOException;
import java.nio.ByteBuffer;

public class Main {
    public static ByteBuffer readFile(File file) throws IOException {
        byte[] buffer = new byte[(int) file.length()];
        int bytesRead;
        try (FileInputStream fileReader = new FileInputStream(file)) {
            bytesRead = fileReader.read(buffer);
        }
        assert bytesRead == file.length() : file;
        return ByteBuffer.wrap(buffer);
    }
}

Related

  1. readDouble(final byte[] bytes, final int length, final int offset)
  2. readEntireFile(java.io.File file)
  3. readerToString(Reader reader)
  4. readFC(String fname, int length)
  5. readFile(File file)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(File file)
  9. readFile(File file)