jdk/src/share/classes/java/io/FileWriter.java
author mchung
Thu, 12 Mar 2009 10:27:22 -0700
changeset 2277 445a331b4a8b
parent 2 90ce3da70b43
child 5506 202f599c92aa
permissions -rw-r--r--
6810254: Lazily instantiate the shared secret access objects Summary: Register the shutdown hooks only when needed and remove JavaIODeleteOnExitAccess Reviewed-by: alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1996-2001 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package java.io;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * Convenience class for writing character files.  The constructors of this
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * class assume that the default character encoding and the default byte-buffer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * size are acceptable.  To specify these values yourself, construct an
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * OutputStreamWriter on a FileOutputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>Whether or not a file is available or may be created depends upon the
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * underlying platform.  Some platforms, in particular, allow a file to be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * opened for writing by only one <tt>FileWriter</tt> (or other file-writing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * object) at a time.  In such situations the constructors in this class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * will fail if the file involved is already open.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * <p><code>FileWriter</code> is meant for writing streams of characters.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * For writing streams of raw bytes, consider using a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * <code>FileOutputStream</code>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 * @see OutputStreamWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 * @see FileOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 * @author      Mark Reinhold
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * @since       JDK1.1
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
public class FileWriter extends OutputStreamWriter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
     * Constructs a FileWriter object given a file name.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     * @param fileName  String The system-dependent filename.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
     * @throws IOException  if the named file exists but is a directory rather
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
     *                  than a regular file, does not exist but cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
     *                  created, or cannot be opened for any other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
    public FileWriter(String fileName) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        super(new FileOutputStream(fileName));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
     * Constructs a FileWriter object given a file name with a boolean
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
     * indicating whether or not to append the data written.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
     * @param fileName  String The system-dependent filename.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
     * @param append    boolean if <code>true</code>, then data will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
     *                  to the end of the file rather than the beginning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
     * @throws IOException  if the named file exists but is a directory rather
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
     *                  than a regular file, does not exist but cannot be
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
     *                  created, or cannot be opened for any other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    public FileWriter(String fileName, boolean append) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        super(new FileOutputStream(fileName, append));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
     * Constructs a FileWriter object given a File object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
     * @param file  a File object to write to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
     * @throws IOException  if the file exists but is a directory rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
     *                  a regular file, does not exist but cannot be created,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
     *                  or cannot be opened for any other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public FileWriter(File file) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        super(new FileOutputStream(file));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * Constructs a FileWriter object given a File object. If the second
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * argument is <code>true</code>, then bytes will be written to the end
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * of the file rather than the beginning.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param file  a File object to write to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param     append    if <code>true</code>, then bytes will be written
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     *                      to the end of the file rather than the beginning
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @throws IOException  if the file exists but is a directory rather than
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     *                  a regular file, does not exist but cannot be created,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     *                  or cannot be opened for any other reason
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    public FileWriter(File file, boolean append) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        super(new FileOutputStream(file, append));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * Constructs a FileWriter object associated with a file descriptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * @param fd  FileDescriptor object to write to.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
    public FileWriter(FileDescriptor fd) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        super(new FileOutputStream(fd));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
}