Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/java/io/FileWriter.java
38829 views
/*1* Copyright (c) 1996, 2001, 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;262728/**29* Convenience class for writing character files. The constructors of this30* class assume that the default character encoding and the default byte-buffer31* size are acceptable. To specify these values yourself, construct an32* OutputStreamWriter on a FileOutputStream.33*34* <p>Whether or not a file is available or may be created depends upon the35* underlying platform. Some platforms, in particular, allow a file to be36* opened for writing by only one <tt>FileWriter</tt> (or other file-writing37* object) at a time. In such situations the constructors in this class38* will fail if the file involved is already open.39*40* <p><code>FileWriter</code> is meant for writing streams of characters.41* For writing streams of raw bytes, consider using a42* <code>FileOutputStream</code>.43*44* @see OutputStreamWriter45* @see FileOutputStream46*47* @author Mark Reinhold48* @since JDK1.149*/5051public class FileWriter extends OutputStreamWriter {5253/**54* Constructs a FileWriter object given a file name.55*56* @param fileName String The system-dependent filename.57* @throws IOException if the named file exists but is a directory rather58* than a regular file, does not exist but cannot be59* created, or cannot be opened for any other reason60*/61public FileWriter(String fileName) throws IOException {62super(new FileOutputStream(fileName));63}6465/**66* Constructs a FileWriter object given a file name with a boolean67* indicating whether or not to append the data written.68*69* @param fileName String The system-dependent filename.70* @param append boolean if <code>true</code>, then data will be written71* to the end of the file rather than the beginning.72* @throws IOException if the named file exists but is a directory rather73* than a regular file, does not exist but cannot be74* created, or cannot be opened for any other reason75*/76public FileWriter(String fileName, boolean append) throws IOException {77super(new FileOutputStream(fileName, append));78}7980/**81* Constructs a FileWriter object given a File object.82*83* @param file a File object to write to.84* @throws IOException if the file exists but is a directory rather than85* a regular file, does not exist but cannot be created,86* or cannot be opened for any other reason87*/88public FileWriter(File file) throws IOException {89super(new FileOutputStream(file));90}9192/**93* Constructs a FileWriter object given a File object. If the second94* argument is <code>true</code>, then bytes will be written to the end95* of the file rather than the beginning.96*97* @param file a File object to write to98* @param append if <code>true</code>, then bytes will be written99* to the end of the file rather than the beginning100* @throws IOException if the file exists but is a directory rather than101* a regular file, does not exist but cannot be created,102* or cannot be opened for any other reason103* @since 1.4104*/105public FileWriter(File file, boolean append) throws IOException {106super(new FileOutputStream(file, append));107}108109/**110* Constructs a FileWriter object associated with a file descriptor.111*112* @param fd FileDescriptor object to write to.113*/114public FileWriter(FileDescriptor fd) {115super(new FileOutputStream(fd));116}117118}119120121