Java File Read via ByteBuffer readByte(String filePath)

Here you can find the source of readByte(String filePath)

Description

read Byte

License

Apache License

Declaration

public static byte[] readByte(String filePath) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
    public static byte[] readByte(String filePath) {
        try {//ww  w.  ja  va  2s  .  c  o  m
            FileInputStream fis = new FileInputStream(filePath);
            FileChannel channel = fis.getChannel();
            ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
            while ((channel.read(byteBuffer)) > 0) {
                // do nothing
                //              System.out.println("reading");
            }
            channel.close();
            fis.close();
            return byteBuffer.array();
        } catch (IOException e) {
            e.printStackTrace();

        }
        return null;
    }
}

Related

  1. readAndTransfer(final String dir)
  2. readAsByteArray(final InputStream source)
  3. readBigEndianWord(byte[] buf)
  4. readBinaryFile(File file)
  5. readBinaryFileAsFloats(String fileName)
  6. readBytes(File f)
  7. readBytes(final InputStream in, final int length)
  8. readBytes(final String file)
  9. readBytes(InputStream is)