Java File Read via ByteBuffer readToBuffer(final String filename)

Here you can find the source of readToBuffer(final String filename)

Description

Get byte buffer from file path.

License

Open Source License

Parameter

Parameter Description
filename a parameter

Exception

Parameter Description
IOException an exception

Declaration

public static ByteBuffer readToBuffer(final String filename) throws IOException 

Method Source Code


//package com.java2s;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;

public class Main {
    /**/*www . j a  va 2 s. c  o  m*/
     * Get byte buffer from file path.
     *
     * @param filename
     * @return
     * @throws IOException
     */
    public static ByteBuffer readToBuffer(final String filename) throws IOException {
        final File file = new File(filename);
        final ByteBuffer bytebBuffer = ByteBuffer.allocate((int) file.length());
        final FileInputStream fis = new FileInputStream(filename);
        bytebBuffer.rewind();
        int b = 0;
        final byte[] buf = new byte[1024];

        while ((b = fis.read()) != -1) {
            bytebBuffer.put((byte) b);
        }

        return bytebBuffer;
    }
}

Related

  1. readString(ByteArrayInputStream bin)
  2. readString(ReadableByteChannel channel)
  3. readStringFromFile(String path)
  4. readStringFromSocketChannel(SocketChannel sc)
  5. readStringInUTF8(DataInput in)
  6. readToByteArray(String fname)
  7. readTwoByteInt(byte[] array, int start)
  8. readTxtFromFile(File file)
  9. readUint32(final DataInput di)