Java InputStream Read getInputStreamFromFile(File pFile)

Here you can find the source of getInputStreamFromFile(File pFile)

Description

Gets an InputStream for the contents of a file - if the file is .gz it will return a stream of the decompressed contents

License

Apache License

Parameter

Parameter Description
pFile file to open

Return

stream (or null)

Declaration

public static InputStream getInputStreamFromFile(File pFile) 

Method Source Code


//package com.java2s;
/*/*from  w  w w  .jav  a  2 s . c  om*/
 * Copyright 2014 The British Library/SCAPE Project Consortium
 * Author: William Palmer (William.Palmer@bl.uk)
 *
 *   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;

import java.io.IOException;
import java.io.InputStream;
import java.util.zip.GZIPInputStream;

public class Main {
    /**
     * Gets an InputStream for the contents of a file - if the file is .gz it will return
     * a stream of the decompressed contents
     * @param pFile file to open
     * @return stream (or null)
     */
    public static InputStream getInputStreamFromFile(File pFile) {

        if (!pFile.exists())
            return null;

        try {
            return new GZIPInputStream(new FileInputStream(pFile));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            try {
                return new FileInputStream(pFile);
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }

        return null;

    }
}

Related

  1. getInputStreamForFileAbsolutePath(String _path)
  2. getInputStreamForResource(String name)
  3. getInputStreamForResource(String relativePath)
  4. getInputStreamFromAbsolutePath(String resource)
  5. getInputStreamFromClassPath(String filename)
  6. getInputStreamFromFile(final File file)
  7. getInputStreamFromZip(ZipInputStream zis, String filename)
  8. getInputStreamLength(InputStream in)
  9. getInputStreamReader(InputStream in, String charset)