Java DataInputStream Read readFromFile(File file)

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

Description

read From File

License

Open Source License

Declaration

public static byte[] readFromFile(File file) 

Method Source Code

//package com.java2s;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;

public class Main {
    public static byte[] readFromFile(File file) {
        long fileSize = file.length();
        byte[] fileData = new byte[(int) fileSize];

        FileInputStream fileIn;//ww  w .jav a  2s.c om
        DataInputStream dataIn;
        int offset = 0;
        int numRead = 0;
        try {
            fileIn = new FileInputStream(file);
            dataIn = new DataInputStream(fileIn);
            while (offset < fileData.length
                    && (numRead = dataIn.read(fileData, offset,
                            fileData.length - offset)) >= 0) {
                offset += numRead;
            }

            dataIn.close();
            fileIn.close();
            return fileData;
        } catch (Exception ex) {
            return null;
        }
    }
}

Related

  1. readBytes(DataInputStream stream, int size)
  2. readBytes(File ff)
  3. readBytes(String filename)
  4. readBytes(String filename)
  5. readBytesArray(DataInputStream dis)
  6. readFromFileToLineArray(File file)
  7. saveDoubleMatrixFromBinary(DataInputStream reader, int rows, int cols, PrintStream out)