Path: blob/master/src/java.base/share/classes/java/io/FileReader.java
41152 views
/*1* Copyright (c) 1996, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package java.io;2627import java.nio.charset.Charset;2829/**30* Reads text from character files using a default buffer size. Decoding from bytes31* to characters uses either a specified {@linkplain java.nio.charset.Charset charset}32* or the platform's33* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.34*35* <p>36* The {@code FileReader} is meant for reading streams of characters. For reading37* streams of raw bytes, consider using a {@code FileInputStream}.38*39* @see InputStreamReader40* @see FileInputStream41*42* @author Mark Reinhold43* @since 1.144*/45public class FileReader extends InputStreamReader {4647/**48* Creates a new {@code FileReader}, given the name of the file to read,49* using the platform's50* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.51*52* @param fileName the name of the file to read53* @throws FileNotFoundException if the named file does not exist,54* is a directory rather than a regular file,55* or for some other reason cannot be opened for56* reading.57*/58public FileReader(String fileName) throws FileNotFoundException {59super(new FileInputStream(fileName));60}6162/**63* Creates a new {@code FileReader}, given the {@code File} to read,64* using the platform's65* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.66*67* @param file the {@code File} to read68* @throws FileNotFoundException if the file does not exist,69* is a directory rather than a regular file,70* or for some other reason cannot be opened for71* reading.72*/73public FileReader(File file) throws FileNotFoundException {74super(new FileInputStream(file));75}7677/**78* Creates a new {@code FileReader}, given the {@code FileDescriptor} to read,79* using the platform's80* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.81*82* @param fd the {@code FileDescriptor} to read83*/84public FileReader(FileDescriptor fd) {85super(new FileInputStream(fd));86}8788/**89* Creates a new {@code FileReader}, given the name of the file to read90* and the {@linkplain java.nio.charset.Charset charset}.91*92* @param fileName the name of the file to read93* @param charset the {@linkplain java.nio.charset.Charset charset}94* @throws IOException if the named file does not exist,95* is a directory rather than a regular file,96* or for some other reason cannot be opened for97* reading.98*99* @since 11100*/101public FileReader(String fileName, Charset charset) throws IOException {102super(new FileInputStream(fileName), charset);103}104105/**106* Creates a new {@code FileReader}, given the {@code File} to read and107* the {@linkplain java.nio.charset.Charset charset}.108*109* @param file the {@code File} to read110* @param charset the {@linkplain java.nio.charset.Charset charset}111* @throws IOException if the file does not exist,112* is a directory rather than a regular file,113* or for some other reason cannot be opened for114* reading.115*116* @since 11117*/118public FileReader(File file, Charset charset) throws IOException {119super(new FileInputStream(file), charset);120}121}122123124