Path: blob/master/src/java.base/share/classes/java/io/FileWriter.java
41152 views
/*1* Copyright (c) 1996, 2018, 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* Writes text to character files using a default buffer size. Encoding from characters31* to bytes 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* Whether or not a file is available or may be created depends upon the37* underlying platform. Some platforms, in particular, allow a file to be38* opened for writing by only one {@code FileWriter} (or other file-writing39* object) at a time. In such situations the constructors in this class40* will fail if the file involved is already open.41*42* <p>43* The {@code FileWriter} is meant for writing streams of characters. For writing44* streams of raw bytes, consider using a {@code FileOutputStream}.45*46* @see OutputStreamWriter47* @see FileOutputStream48*49* @author Mark Reinhold50* @since 1.151*/5253public class FileWriter extends OutputStreamWriter {5455/**56* Constructs a {@code FileWriter} given a file name, using the platform's57* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}58*59* @param fileName String The system-dependent filename.60* @throws IOException if the named file exists but is a directory rather61* than a regular file, does not exist but cannot be62* created, or cannot be opened for any other reason63*/64public FileWriter(String fileName) throws IOException {65super(new FileOutputStream(fileName));66}6768/**69* Constructs a {@code FileWriter} given a file name and a boolean indicating70* whether to append the data written, using the platform's71* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.72*73* @param fileName String The system-dependent filename.74* @param append boolean if {@code true}, then data will be written75* to the end of the file rather than the beginning.76* @throws IOException if the named file exists but is a directory rather77* than a regular file, does not exist but cannot be78* created, or cannot be opened for any other reason79*/80public FileWriter(String fileName, boolean append) throws IOException {81super(new FileOutputStream(fileName, append));82}8384/**85* Constructs a {@code FileWriter} given the {@code File} to write,86* using the platform's87* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}88*89* @param file the {@code File} to write.90* @throws IOException if the file exists but is a directory rather than91* a regular file, does not exist but cannot be created,92* or cannot be opened for any other reason93*/94public FileWriter(File file) throws IOException {95super(new FileOutputStream(file));96}9798/**99* Constructs a {@code FileWriter} given the {@code File} to write and100* a boolean indicating whether to append the data written, using the platform's101* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.102*103* @param file the {@code File} to write104* @param append if {@code true}, then bytes will be written105* to the end of the file rather than the beginning106* @throws IOException if the file exists but is a directory rather than107* a regular file, does not exist but cannot be created,108* or cannot be opened for any other reason109* @since 1.4110*/111public FileWriter(File file, boolean append) throws IOException {112super(new FileOutputStream(file, append));113}114115/**116* Constructs a {@code FileWriter} given a file descriptor,117* using the platform's118* {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.119*120* @param fd the {@code FileDescriptor} to write.121*/122public FileWriter(FileDescriptor fd) {123super(new FileOutputStream(fd));124}125126127/**128* Constructs a {@code FileWriter} given a file name and129* {@linkplain java.nio.charset.Charset charset}.130*131* @param fileName the name of the file to write132* @param charset the {@linkplain java.nio.charset.Charset charset}133* @throws IOException if the named file exists but is a directory rather134* than a regular file, does not exist but cannot be135* created, or cannot be opened for any other reason136*137* @since 11138*/139public FileWriter(String fileName, Charset charset) throws IOException {140super(new FileOutputStream(fileName), charset);141}142143/**144* Constructs a {@code FileWriter} given a file name,145* {@linkplain java.nio.charset.Charset charset} and a boolean indicating146* whether to append the data written.147*148* @param fileName the name of the file to write149* @param charset the {@linkplain java.nio.charset.Charset charset}150* @param append a boolean. If {@code true}, the writer will write the data151* to the end of the file rather than the beginning.152* @throws IOException if the named file exists but is a directory rather153* than a regular file, does not exist but cannot be154* created, or cannot be opened for any other reason155*156* @since 11157*/158public FileWriter(String fileName, Charset charset, boolean append) throws IOException {159super(new FileOutputStream(fileName, append), charset);160}161162/**163* Constructs a {@code FileWriter} given the {@code File} to write and164* {@linkplain java.nio.charset.Charset charset}.165*166* @param file the {@code File} to write167* @param charset the {@linkplain java.nio.charset.Charset charset}168* @throws IOException if the file exists but is a directory rather than169* a regular file, does not exist but cannot be created,170* or cannot be opened for any other reason171*172* @since 11173*/174public FileWriter(File file, Charset charset) throws IOException {175super(new FileOutputStream(file), charset);176}177178/**179* Constructs a {@code FileWriter} given the {@code File} to write,180* {@linkplain java.nio.charset.Charset charset} and a boolean indicating181* whether to append the data written.182*183* @param file the {@code File} to write184* @param charset the {@linkplain java.nio.charset.Charset charset}185* @param append a boolean. If {@code true}, the writer will write the data186* to the end of the file rather than the beginning.187* @throws IOException if the file exists but is a directory rather than188* a regular file, does not exist but cannot be created,189* or cannot be opened for any other reason190* @since 11191*/192public FileWriter(File file, Charset charset, boolean append) throws IOException {193super(new FileOutputStream(file, append), charset);194}195}196197198