Java File to InputStream openInputStream(File file, boolean refuseEmpty)

Here you can find the source of openInputStream(File file, boolean refuseEmpty)

Description

open Input Stream

License

Apache License

Parameter

Parameter Description
file a parameter
refuseEmpty whether or not to refuse empty files (i.e. size = 0 bytes)

Exception

Parameter Description
IllegalArgumentException when the file is empty and refuseEmpty is true
FileNotFoundException when the file does not exist
SecurityException when we are not allowed to read the file
NullPointerException when file is null

Return

a FileInputStream

Declaration

static public FileInputStream openInputStream(File file, boolean refuseEmpty)
        throws IllegalArgumentException, FileNotFoundException, SecurityException, NullPointerException 

Method Source Code

//package com.java2s;
/**//from   ww w .ja  va 2 s  .c om
 * Sapelli data collection platform: http://sapelli.org
 * 
 * Copyright 2012-2016 University College London - ExCiteS group
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */

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

public class Main {
    /**
     * @param file
     * @param refuseEmpty whether or not to refuse empty files (i.e. size = 0 bytes)
     * @return a FileInputStream
     * @throws IllegalArgumentException when the file is empty and refuseEmpty is true
     * @throws FileNotFoundException when the file does not exist
     * @throws SecurityException when we are not allowed to read the file 
     * @throws NullPointerException when file is null
     */
    static public FileInputStream openInputStream(File file, boolean refuseEmpty)
            throws IllegalArgumentException, FileNotFoundException, SecurityException, NullPointerException {
        if (file != null && file.exists() && refuseEmpty && file.length() == 0)
            throw new IllegalArgumentException("File \"" + file.getAbsolutePath() + "\" is empty!");
        return new FileInputStream(file);
    }
}

Related

  1. openInputStream(File file)
  2. openInputStream(File file)
  3. openInputStream(File file)
  4. openInputStream(File file)
  5. openInputStream(File file, boolean buffer)
  6. openInputStream(String filePath)
  7. openInputStream(String infilename)
  8. openInputStream(String path)
  9. openStream(final Class clazz, final String path)