Java File to Reader openReader(File file)

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

Description

Opens a Reader on a file.

License

Open Source License

Parameter

Parameter Description
file the file to open the reader on

Exception

Parameter Description
NullPointerException if file is null

Return

the reader, or null if the file could not be opened

Declaration

public static Reader openReader(File file) 

Method Source Code

//package com.java2s;
/*//from  w  w w  .j av  a  2  s. co m
 * $Id$
 *
 * Copyright (C) 2003-2015 JNode.org
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; either version 2.1 of the License, or
 * (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
 * License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; If not, write to the Free Software Foundation, Inc., 
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.Reader;

public class Main {
    private static final String ex_null_param = "A required paramater is null";

    /**
     * Opens a Reader on a file.
     * 
     * This method will not throw a FileNotFoundException like the FileReader
     * constructor would.
     *
     * @param file the file to open the reader on
     * @return the reader, or null if the file could not be opened
     * @throws NullPointerException if file is null
     */
    public static Reader openReader(File file) {
        checkNull(file);

        try {
            return new FileReader(file);
        } catch (FileNotFoundException e) {
            // fall through
        }

        return null;
    }

    /**
     * Check for null objects in a list of objects.
     */
    private static void checkNull(Object... objs) {
        for (Object obj : objs) {
            if (obj == null)
                throw new NullPointerException(ex_null_param);
        }
    }
}

Related

  1. newReader(File file)
  2. newReader(File file, String charset)
  3. newReader(File file, String encoding)
  4. newReader(File pathname)
  5. newReader(String value)
  6. openReader(final String file, String charset)
  7. openReader(String dir, String filename, String encoding)
  8. openReader(String file)
  9. openReader(String filePath)