src/java.base/share/classes/java/io/FileWriter.java
changeset 49254 422615764e12
parent 47216 71c04702a3d5
equal deleted inserted replaced
49253:26f624b33218 49254:422615764e12
     1 /*
     1 /*
     2  * Copyright (c) 1996, 2001, Oracle and/or its affiliates. All rights reserved.
     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.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.  Oracle designates this
     7  * published by the Free Software Foundation.  Oracle designates this
    23  * questions.
    23  * questions.
    24  */
    24  */
    25 
    25 
    26 package java.io;
    26 package java.io;
    27 
    27 
       
    28 import java.nio.charset.Charset;
    28 
    29 
    29 /**
    30 /**
    30  * Convenience class for writing character files.  The constructors of this
    31  * Writes text to character files using a default buffer size. Encoding from characters
    31  * class assume that the default character encoding and the default byte-buffer
    32  * to bytes uses either a specified {@linkplain java.nio.charset.Charset charset}
    32  * size are acceptable.  To specify these values yourself, construct an
    33  * or the platform's
    33  * OutputStreamWriter on a FileOutputStream.
    34  * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
    34  *
    35  *
    35  * <p>Whether or not a file is available or may be created depends upon the
    36  * <p>
       
    37  * Whether or not a file is available or may be created depends upon the
    36  * underlying platform.  Some platforms, in particular, allow a file to be
    38  * underlying platform.  Some platforms, in particular, allow a file to be
    37  * opened for writing by only one {@code FileWriter} (or other file-writing
    39  * opened for writing by only one {@code FileWriter} (or other file-writing
    38  * object) at a time.  In such situations the constructors in this class
    40  * object) at a time.  In such situations the constructors in this class
    39  * will fail if the file involved is already open.
    41  * will fail if the file involved is already open.
    40  *
    42  *
    41  * <p>{@code FileWriter} is meant for writing streams of characters.
    43  * <p>
    42  * For writing streams of raw bytes, consider using a
    44  * The {@code FileWriter} is meant for writing streams of characters. For writing
    43  * {@code FileOutputStream}.
    45  * streams of raw bytes, consider using a {@code FileOutputStream}.
    44  *
    46  *
    45  * @see OutputStreamWriter
    47  * @see OutputStreamWriter
    46  * @see FileOutputStream
    48  * @see FileOutputStream
    47  *
    49  *
    48  * @author      Mark Reinhold
    50  * @author      Mark Reinhold
    50  */
    52  */
    51 
    53 
    52 public class FileWriter extends OutputStreamWriter {
    54 public class FileWriter extends OutputStreamWriter {
    53 
    55 
    54     /**
    56     /**
    55      * Constructs a FileWriter object given a file name.
    57      * Constructs a {@code FileWriter} given a file name, using the platform's
       
    58      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}
    56      *
    59      *
    57      * @param fileName  String The system-dependent filename.
    60      * @param fileName  String The system-dependent filename.
    58      * @throws IOException  if the named file exists but is a directory rather
    61      * @throws IOException  if the named file exists but is a directory rather
    59      *                  than a regular file, does not exist but cannot be
    62      *                  than a regular file, does not exist but cannot be
    60      *                  created, or cannot be opened for any other reason
    63      *                  created, or cannot be opened for any other reason
    62     public FileWriter(String fileName) throws IOException {
    65     public FileWriter(String fileName) throws IOException {
    63         super(new FileOutputStream(fileName));
    66         super(new FileOutputStream(fileName));
    64     }
    67     }
    65 
    68 
    66     /**
    69     /**
    67      * Constructs a FileWriter object given a file name with a boolean
    70      * Constructs a {@code FileWriter} given a file name and a boolean indicating
    68      * indicating whether or not to append the data written.
    71      * whether to append the data written, using the platform's
       
    72      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
    69      *
    73      *
    70      * @param fileName  String The system-dependent filename.
    74      * @param fileName  String The system-dependent filename.
    71      * @param append    boolean if {@code true}, then data will be written
    75      * @param append    boolean if {@code true}, then data will be written
    72      *                  to the end of the file rather than the beginning.
    76      *                  to the end of the file rather than the beginning.
    73      * @throws IOException  if the named file exists but is a directory rather
    77      * @throws IOException  if the named file exists but is a directory rather
    77     public FileWriter(String fileName, boolean append) throws IOException {
    81     public FileWriter(String fileName, boolean append) throws IOException {
    78         super(new FileOutputStream(fileName, append));
    82         super(new FileOutputStream(fileName, append));
    79     }
    83     }
    80 
    84 
    81     /**
    85     /**
    82      * Constructs a FileWriter object given a File object.
    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}
    83      *
    89      *
    84      * @param file  a File object to write to.
    90      * @param file  the {@code File} to write.
    85      * @throws IOException  if the file exists but is a directory rather than
    91      * @throws IOException  if the file exists but is a directory rather than
    86      *                  a regular file, does not exist but cannot be created,
    92      *                  a regular file, does not exist but cannot be created,
    87      *                  or cannot be opened for any other reason
    93      *                  or cannot be opened for any other reason
    88      */
    94      */
    89     public FileWriter(File file) throws IOException {
    95     public FileWriter(File file) throws IOException {
    90         super(new FileOutputStream(file));
    96         super(new FileOutputStream(file));
    91     }
    97     }
    92 
    98 
    93     /**
    99     /**
    94      * Constructs a FileWriter object given a File object. If the second
   100      * Constructs a {@code FileWriter} given the {@code File} to write and
    95      * argument is {@code true}, then bytes will be written to the end
   101      * a boolean indicating whether to append the data written, using the platform's
    96      * of the file rather than the beginning.
   102      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
    97      *
   103      *
    98      * @param file  a File object to write to
   104      * @param file  the {@code File} to write
    99      * @param     append    if {@code true}, then bytes will be written
   105      * @param     append    if {@code true}, then bytes will be written
   100      *                      to the end of the file rather than the beginning
   106      *                      to the end of the file rather than the beginning
   101      * @throws IOException  if the file exists but is a directory rather than
   107      * @throws IOException  if the file exists but is a directory rather than
   102      *                  a regular file, does not exist but cannot be created,
   108      *                  a regular file, does not exist but cannot be created,
   103      *                  or cannot be opened for any other reason
   109      *                  or cannot be opened for any other reason
   106     public FileWriter(File file, boolean append) throws IOException {
   112     public FileWriter(File file, boolean append) throws IOException {
   107         super(new FileOutputStream(file, append));
   113         super(new FileOutputStream(file, append));
   108     }
   114     }
   109 
   115 
   110     /**
   116     /**
   111      * Constructs a FileWriter object associated with a file descriptor.
   117      * Constructs a {@code FileWriter} given a file descriptor,
       
   118      * using the platform's
       
   119      * {@linkplain java.nio.charset.Charset#defaultCharset() default charset}.
   112      *
   120      *
   113      * @param fd  FileDescriptor object to write to.
   121      * @param fd  the {@code FileDescriptor} to write.
   114      */
   122      */
   115     public FileWriter(FileDescriptor fd) {
   123     public FileWriter(FileDescriptor fd) {
   116         super(new FileOutputStream(fd));
   124         super(new FileOutputStream(fd));
   117     }
   125     }
   118 
   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     }
   119 }
   196 }