Java File to Reader openReader(String fname)

Here you can find the source of openReader(String fname)

Description

open Reader

License

Open Source License

Declaration

public static BufferedReader openReader(String fname) throws IOException 

Method Source Code

//package com.java2s;
/**//w  ww. j  a  va2  s. c om
 * This software is released under the University of Illinois/Research and Academic Use License. See
 * the LICENSE file in the root folder for details. Copyright (c) 2016
 *
 * Developed by: The Cognitive Computation Group University of Illinois at Urbana-Champaign
 * http://cogcomp.cs.illinois.edu/
 */

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Main {
    public static BufferedReader openReader(String fname) throws IOException {
        BufferedReader reader;

        InputStream is = ClassLoader.getSystemResourceAsStream(fname);

        if (is == null) {
            // try with a leading slash
            is = ClassLoader.getSystemResourceAsStream("/" + fname);

            if (is == null)
                is = new FileInputStream(fname);

        }

        reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
        return reader;

    }
}

Related

  1. openReader(File file)
  2. openReader(final String file, String charset)
  3. openReader(String dir, String filename, String encoding)
  4. openReader(String file)
  5. openReader(String filePath)