Java FileInputStream Read readFile(File f)

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

Description

read File

License

Open Source License

Declaration

public static final byte[] readFile(File f) throws IOException 

Method Source Code


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

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

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class Main {
    public static final byte[] readFile(File f) throws IOException {
        int len = f.length() > Integer.MAX_VALUE ? 8192 : (int) f.length();
        byte[] buf = new byte[len];
        FileInputStream is = new FileInputStream(f);
        try {//  w  ww. ja  v a2 s .c o  m
            ByteArrayOutputStream os = new ByteArrayOutputStream((int) f.length());
            int n = is.read(buf);
            while (n != -1) {
                os.write(buf, 0, n);
                n = is.read(buf);
            }
            return os.toByteArray();
        } finally {
            is.close();
        }
    }
}

Related

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