Java File Read via ByteBuffer readBinaryFileAsFloats(String fileName)

Here you can find the source of readBinaryFileAsFloats(String fileName)

Description

read Binary File As Floats

License

Open Source License

Declaration

private static float[] readBinaryFileAsFloats(String fileName)
            throws IOException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayOutputStream;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;

public class Main {
    private static float[] readBinaryFileAsFloats(String fileName)
            throws IOException {
        FileInputStream fis = new FileInputStream(new File(fileName));
        byte data[] = readFully(fis);
        ByteBuffer bb = ByteBuffer.wrap(data);
        bb.order(ByteOrder.nativeOrder());
        FloatBuffer fb = bb.asFloatBuffer();
        float result[] = new float[fb.capacity()];
        fb.get(result);/*from   w w  w  .ja va  2s .com*/
        return result;
    }

    private static byte[] readFully(InputStream inputStream)
            throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte buffer[] = new byte[1024];
        while (true) {
            int n = inputStream.read(buffer);
            if (n < 0) {
                break;
            }
            baos.write(buffer, 0, n);
        }
        byte data[] = baos.toByteArray();
        return data;
    }
}

Related

  1. readAiffHeader(DataInputStream inStrm, FileChannel fc)
  2. readAndTransfer(final String dir)
  3. readAsByteArray(final InputStream source)
  4. readBigEndianWord(byte[] buf)
  5. readBinaryFile(File file)
  6. readByte(String filePath)
  7. readBytes(File f)
  8. readBytes(final InputStream in, final int length)
  9. readBytes(final String file)