Java FileInputStream Read readFile(File file)

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

Description

read File

License

Apache License

Declaration

public static byte[] readFile(File file) 

Method Source Code


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

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

public class Main {
    public static byte[] readFile(File file) {
        try {/* ww  w.jav a 2  s  .  c  om*/
            if (file.length() > 1 << 30) {
                throw new ArrayIndexOutOfBoundsException("File is too big");
            }
            byte[] data = new byte[(int) file.length()];
            FileInputStream fis = new FileInputStream(file);
            try {
                int n = 0;
                while (n < data.length) {
                    int m = fis.read(data, n, data.length - n);
                    if (m < 0) {
                        throw new RuntimeException("Cannot read file: " + file.getCanonicalPath());
                    }
                    n += m;
                }
            } finally {
                try {
                    fis.close();
                } catch (IOException e) {
                    // ignore
                }
            }
            return data;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. readFile(File file)
  2. readFile(File file)
  3. readFile(File file)
  4. readFile(File file)
  5. readFile(File file)
  6. readFile(File file)
  7. readFile(File file)
  8. readFile(File file)
  9. readFile(File file)