Java InputStream Create asInputStream(File file)

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

Description

Return an input stream to the file.

License

Apache License

Parameter

Parameter Description
file The file to get an input stream for.

Exception

Parameter Description
FileNotFoundException If the file doesn't exist.

Return

An input stream to the file.

Declaration

public static InputStream asInputStream(File file) throws FileNotFoundException 

Method Source Code


//package com.java2s;
/*/*  w  ww.  j  ava 2  s . c om*/
 * Copyright JTheque (Baptiste Wicht)
 *
 * 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.BufferedInputStream;

import java.io.File;

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

import java.io.InputStream;

public class Main {
    /**
     * The default size of buffer.
     */
    private static final int BUFFER_SIZE = 2048;

    /**
     * Return an input stream to the path. The stream will be buffered.
     *
     * @param path The path to get an input stream for.
     *
     * @return An input stream to the file path.
     *
     * @throws FileNotFoundException If the path doesn't represent an existing file.
     */
    public static InputStream asInputStream(String path) throws FileNotFoundException {
        return new BufferedInputStream(new FileInputStream(path), BUFFER_SIZE);
    }

    /**
     * Return an input stream to the file. The stream will be buffered.
     *
     * @param file The file to get an input stream for.
     *
     * @return An input stream to the file.
     *
     * @throws FileNotFoundException If the file doesn't exist.
     */
    public static InputStream asInputStream(File file) throws FileNotFoundException {
        return new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);
    }
}

Related

  1. asInputStream(byte[] bytes)
  2. asInputStream(Object content)
  3. asInputStream(String str)
  4. getInputStream(File f)
  5. getInputStream(File jarFile, String fileName)