Java FileInputStream Read readFile(File source)

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

Description

read File

License

Open Source License

Declaration

private static byte[] readFile(File source) throws IOException 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2010-2015 BSI Business Systems Integration AG.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors://  w w w  .ja va2s  .  c  om
 *     BSI Business Systems Integration AG - initial API and implementation
 ******************************************************************************/

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

import java.io.IOException;

public class Main {
    private static byte[] readFile(File source) throws IOException {
        if (!source.exists()) {
            throw new FileNotFoundException(source.getAbsolutePath());
        }
        if (!source.canRead()) {
            throw new IOException("cannot read " + source);
        }
        if (source.isDirectory()) {
            // source can not be a directory
            throw new IOException("source is a directory: " + source);
        }
        try (FileInputStream input = new FileInputStream(source)) {
            byte[] data = new byte[(int) source.length()];
            int n = 0;
            while (n < data.length) {
                n += input.read(data, n, data.length - n);
            }
            return data;
        }
    }
}

Related

  1. readFile(File file, String encoding)
  2. readFile(File file, String encoding)
  3. readFile(File fyl)
  4. readFile(File path)
  5. readfile(File path)
  6. readFile(File src)
  7. readFile(File srcFile, OutputStream destStream)
  8. readFile(File target)
  9. readFile(FileInputStream fileinputstream, byte abyte0[])