1 /*
2  * Copyright (c) 1996, 2018, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */

25
26 package java.io;
27
28 import java.nio.charset.Charset;
29
30 /**
31  * Writes text to character files using a default buffer size. Encoding from characters
32  * to bytes uses either a specified {@linkplain java.nio.charset.Charset charset}
33  * or the platform's
34  * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
35  *
36  * <p>
37  * Whether or not a file is available or may be created depends upon the
38  * underlying platform.  Some platforms, in particular, allow a file to be
39  * opened for writing by only one {@code FileWriter} (or other file-writing
40  * object) at a time.  In such situations the constructors in this class
41  * will fail if the file involved is already open.
42  *
43  * <p>
44  * The {@code FileWriter} is meant for writing streams of characters. For writing
45  * streams of raw bytes, consider using a {@code FileOutputStream}.
46  *
47  * @see OutputStreamWriter
48  * @see FileOutputStream
49  *
50  * @author      Mark Reinhold
51  * @since       1.1
52  */

53
54 public class FileWriter extends OutputStreamWriter {
55
56     /**
57      * Constructs a {@code FileWriter} given a file name, using the platform's
58      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}
59      *
60      * @param fileName  String The system-dependent filename.
61      * @throws IOException  if the named file exists but is a directory rather
62      *                  than a regular file, does not exist but cannot be
63      *                  created, or cannot be opened for any other reason
64      */

65     public FileWriter(String fileName) throws IOException {
66         super(new FileOutputStream(fileName));
67     }
68
69     /**
70      * Constructs a {@code FileWriter} given a file name and a boolean indicating
71      * whether to append the data written, using the platform's
72      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
73      *
74      * @param fileName  String The system-dependent filename.
75      * @param append    boolean if {@code true}, then data will be written
76      *                  to the end of the file rather than the beginning.
77      * @throws IOException  if the named file exists but is a directory rather
78      *                  than a regular file, does not exist but cannot be
79      *                  created, or cannot be opened for any other reason
80      */

81     public FileWriter(String fileName, boolean append) throws IOException {
82         super(new FileOutputStream(fileName, append));
83     }
84
85     /**
86      * Constructs a {@code FileWriter} given the {@code File} to write,
87      * using the platform's
88      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}
89      *
90      * @param file  the {@code File} to write.
91      * @throws IOException  if the file exists but is a directory rather than
92      *                  a regular file, does not exist but cannot be created,
93      *                  or cannot be opened for any other reason
94      */

95     public FileWriter(File file) throws IOException {
96         super(new FileOutputStream(file));
97     }
98
99     /**
100      * Constructs a {@code FileWriter} given the {@code File} to write and
101      * a boolean indicating whether to append the data written, using the platform's
102      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
103      *
104      * @param file  the {@code File} to write
105      * @param     append    if {@code true}, then bytes will be written
106      *                      to the end of the file rather than the beginning
107      * @throws IOException  if the file exists but is a directory rather than
108      *                  a regular file, does not exist but cannot be created,
109      *                  or cannot be opened for any other reason
110      * @since 1.4
111      */

112     public FileWriter(File file, boolean append) throws IOException {
113         super(new FileOutputStream(file, append));
114     }
115
116     /**
117      * Constructs a {@code FileWriter} given a file descriptor,
118      * using the platform's
119      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
120      *
121      * @param fd  the {@code FileDescriptor} to write.
122      */

123     public FileWriter(FileDescriptor fd) {
124         super(new FileOutputStream(fd));
125     }
126
127
128     /**
129      * Constructs a {@code FileWriter} given a file name and
130      * {@linkplain java.nio.charset.Charset charset}.
131      *
132      * @param fileName  the name of the file to write
133      * @param charset the {@linkplain java.nio.charset.Charset charset}
134      * @throws IOException  if the named file exists but is a directory rather
135      *                  than a regular file, does not exist but cannot be
136      *                  created, or cannot be opened for any other reason
137      *
138      * @since 11
139      */

140     public FileWriter(String fileName, Charset charset) throws IOException {
141         super(new FileOutputStream(fileName), charset);
142     }
143
144     /**
145      * Constructs a {@code FileWriter} given a file name,
146      * {@linkplain java.nio.charset.Charset charset} and a boolean indicating
147      * whether to append the data written.
148      *
149      * @param fileName  the name of the file to write
150      * @param charset the {@linkplain java.nio.charset.Charset charset}
151      * @param append    a boolean. If {@code true}, the writer will write the data
152      *                  to the end of the file rather than the beginning.
153      * @throws IOException  if the named file exists but is a directory rather
154      *                  than a regular file, does not exist but cannot be
155      *                  created, or cannot be opened for any other reason
156      *
157      * @since 11
158      */

159     public FileWriter(String fileName, Charset charset, boolean append) throws IOException {
160         super(new FileOutputStream(fileName, append), charset);
161     }
162
163     /**
164      * Constructs a {@code FileWriter} given the {@code File} to write and
165      * {@linkplain java.nio.charset.Charset charset}.
166      *
167      * @param file  the {@code File} to write
168      * @param charset the {@linkplain java.nio.charset.Charset charset}
169      * @throws IOException  if the file exists but is a directory rather than
170      *                  a regular file, does not exist but cannot be created,
171      *                  or cannot be opened for any other reason
172      *
173      * @since 11
174      */

175     public FileWriter(File file, Charset charset) throws IOException {
176         super(new FileOutputStream(file), charset);
177     }
178
179     /**
180      * Constructs a {@code FileWriter} given the {@code File} to write,
181      * {@linkplain java.nio.charset.Charset charset} and a boolean indicating
182      * whether to append the data written.
183      *
184      * @param file  the {@code File} to write
185      * @param charset the {@linkplain java.nio.charset.Charset charset}
186      * @param append    a boolean. If {@code true}, the writer will write the data
187      *                  to the end of the file rather than the beginning.
188      * @throws IOException  if the file exists but is a directory rather than
189      *                  a regular file, does not exist but cannot be created,
190      *                  or cannot be opened for any other reason
191      * @since 11
192      */

193     public FileWriter(File file, Charset charset, boolean append) throws IOException {
194         super(new FileOutputStream(file, append), charset);
195     }
196 }
197