1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package java.io; 19 20/** 21 * A specialized {@link Writer} that writes to a file in the file system. 22 * All write requests made by calling methods in this class are directly 23 * forwarded to the equivalent function of the underlying operating system. 24 * Since this may induce some performance penalty, in particular if many small 25 * write requests are made, a FileWriter is often wrapped by a 26 * BufferedWriter. 27 * 28 * @see BufferedWriter 29 * @see FileReader 30 */ 31public class FileWriter extends OutputStreamWriter { 32 33 /** 34 * Creates a FileWriter using the File {@code file}. 35 * 36 * @param file 37 * the non-null File to write bytes to. 38 * @throws IOException 39 * if {@code file} cannot be opened for writing. 40 */ 41 public FileWriter(File file) throws IOException { 42 super(new FileOutputStream(file)); 43 } 44 45 /** 46 * Creates a FileWriter using the File {@code file}. The parameter 47 * {@code append} determines whether or not the file is opened and appended 48 * to or just opened and overwritten. 49 * 50 * @param file 51 * the non-null File to write bytes to. 52 * @param append 53 * indicates whether or not to append to an existing file. 54 * @throws IOException 55 * if the {@code file} cannot be opened for writing. 56 */ 57 public FileWriter(File file, boolean append) throws IOException { 58 super(new FileOutputStream(file, append)); 59 } 60 61 /** 62 * Creates a FileWriter using the existing FileDescriptor {@code fd}. 63 * 64 * @param fd 65 * the non-null FileDescriptor to write bytes to. 66 */ 67 public FileWriter(FileDescriptor fd) { 68 super(new FileOutputStream(fd)); 69 } 70 71 /** 72 * Creates a FileWriter using the platform dependent {@code filename}. 73 * 74 * @param filename 75 * the non-null name of the file to write bytes to. 76 * @throws IOException 77 * if the file cannot be opened for writing. 78 */ 79 public FileWriter(String filename) throws IOException { 80 super(new FileOutputStream(new File(filename))); 81 } 82 83 /** 84 * Creates a FileWriter using the platform dependent {@code filename}. The 85 * parameter {@code append} determines whether or not the file is opened and 86 * appended to or just opened and overwritten. 87 * 88 * @param filename 89 * the non-null name of the file to write bytes to. 90 * @param append 91 * indicates whether or not to append to an existing file. 92 * @throws IOException 93 * if the {@code file} cannot be opened for writing. 94 */ 95 public FileWriter(String filename, boolean append) throws IOException { 96 super(new FileOutputStream(filename, append)); 97 } 98} 99