Java BufferedReader Create getBufferedReader(String filename)

Here you can find the source of getBufferedReader(String filename)

Description

getBufferedReader

License

Apache License

Declaration


public static BufferedReader getBufferedReader(String filename) throws IOException 

Method Source Code

//package com.java2s;
/**File: UtilLib.java //from   ww w .j  a  v a 2  s . c  om
    
Original Author: Sven Schuierer
Date: 04/01/2012
    
Copyright 2015 Novartis Institutes for BioMedical Research
Inc.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.*;

import java.util.zip.*;

public class Main {
    private static int debugLevel = 0;

    /***********************************************************************************
     * 
     *                           getBufferedReader
     *
     ***********************************************************************************/

    public static BufferedReader getBufferedReader(String filename) throws IOException {

        if (filename == "") {
            return null;
        }

        if (filename.equals("-")) {
            System.err.println("Processing input from std in.");
            return new BufferedReader(new InputStreamReader(System.in));
        }

        File inputFile = new File(filename);
        if (!inputFile.exists()) {
            throw new IOException("Warning: file " + filename + " not found.");
        }

        if (!inputFile.canRead()) {
            throw new IOException("Warning: file " + filename + " cannot be read.");
        }

        if (filename.endsWith(".gz")) {
            if (debugLevel >= 2) {
                System.err.println("Return reader for gzipped file.");
            }
            return new BufferedReader(new InputStreamReader(new GZIPInputStream(new FileInputStream(inputFile))));
        }

        return new BufferedReader(new FileReader(inputFile));

    }

    /***********************************************************************************
     * 
     *                           getBufferedReader
     *
     ***********************************************************************************/

    public static BufferedReader getBufferedReader(String filename, boolean check, String fileDescription)
            throws IOException {

        BufferedReader bufferedReader = getBufferedReader(filename);

        if (bufferedReader == null) {
            throw new IOException("ERROR: " + fileDescription + ": \"" + filename + "\" not found.");
        }

        return bufferedReader;

    }
}

Related

  1. getBufferedReader(InputStream stream, String charset)
  2. getBufferedReader(Reader r)
  3. getBufferedReader(Reader rd)
  4. getBufferedReader(String directory, String filename)
  5. getBufferedReader(String file)
  6. getBufferedReader(String filename, String encoding)
  7. getBufferedReader(String nombreFichero)
  8. getBufferedReader(String szFile)
  9. getBufferedReaderAsResource(Class clazz, String fileName)