30
31
32 /**
33 * A file output stream is an output stream for writing data to a
34 * <code>File</code> or to a <code>FileDescriptor</code>. Whether or not
35 * a file is available or may be created depends upon the underlying
36 * platform. Some platforms, in particular, allow a file to be opened
37 * for writing by only one <tt>FileOutputStream</tt> (or other
38 * file-writing object) at a time. In such situations the constructors in
39 * this class will fail if the file involved is already open.
40 *
41 * <p><code>FileOutputStream</code> is meant for writing streams of raw bytes
42 * such as image data. For writing streams of characters, consider using
43 * <code>FileWriter</code>.
44 *
45 * @author Arthur van Hoff
46 * @see java.io.File
47 * @see java.io.FileDescriptor
48 * @see java.io.FileInputStream
49 * @see java.nio.file.Files#newOutputStream
50 * @since JDK1.0
51 */
52 public
53 class FileOutputStream extends OutputStream
54 {
55 /**
56 * The system dependent file descriptor.
57 */
58 private final FileDescriptor fd;
59
60 /**
61 * True if the file is opened for append.
62 */
63 private final boolean append;
64
65 /**
66 * The associated channel, initialized lazily.
67 */
68 private FileChannel channel;
69
70 /**
108 * A new <code>FileDescriptor</code> object is created to represent this
109 * file connection.
110 * <p>
111 * First, if there is a security manager, its <code>checkWrite</code>
112 * method is called with <code>name</code> as its argument.
113 * <p>
114 * If the file exists but is a directory rather than a regular file, does
115 * not exist but cannot be created, or cannot be opened for any other
116 * reason then a <code>FileNotFoundException</code> is thrown.
117 *
118 * @param name the system-dependent file name
119 * @param append if <code>true</code>, then bytes will be written
120 * to the end of the file rather than the beginning
121 * @exception FileNotFoundException if the file exists but is a directory
122 * rather than a regular file, does not exist but cannot
123 * be created, or cannot be opened for any other reason.
124 * @exception SecurityException if a security manager exists and its
125 * <code>checkWrite</code> method denies write access
126 * to the file.
127 * @see java.lang.SecurityManager#checkWrite(java.lang.String)
128 * @since JDK1.1
129 */
130 public FileOutputStream(String name, boolean append)
131 throws FileNotFoundException
132 {
133 this(name != null ? new File(name) : null, append);
134 }
135
136 /**
137 * Creates a file output stream to write to the file represented by
138 * the specified <code>File</code> object. A new
139 * <code>FileDescriptor</code> object is created to represent this
140 * file connection.
141 * <p>
142 * First, if there is a security manager, its <code>checkWrite</code>
143 * method is called with the path represented by the <code>file</code>
144 * argument as its argument.
145 * <p>
146 * If the file exists but is a directory rather than a regular file, does
147 * not exist but cannot be created, or cannot be opened for any other
148 * reason then a <code>FileNotFoundException</code> is thrown.
|
30
31
32 /**
33 * A file output stream is an output stream for writing data to a
34 * <code>File</code> or to a <code>FileDescriptor</code>. Whether or not
35 * a file is available or may be created depends upon the underlying
36 * platform. Some platforms, in particular, allow a file to be opened
37 * for writing by only one <tt>FileOutputStream</tt> (or other
38 * file-writing object) at a time. In such situations the constructors in
39 * this class will fail if the file involved is already open.
40 *
41 * <p><code>FileOutputStream</code> is meant for writing streams of raw bytes
42 * such as image data. For writing streams of characters, consider using
43 * <code>FileWriter</code>.
44 *
45 * @author Arthur van Hoff
46 * @see java.io.File
47 * @see java.io.FileDescriptor
48 * @see java.io.FileInputStream
49 * @see java.nio.file.Files#newOutputStream
50 * @since 1.0
51 */
52 public
53 class FileOutputStream extends OutputStream
54 {
55 /**
56 * The system dependent file descriptor.
57 */
58 private final FileDescriptor fd;
59
60 /**
61 * True if the file is opened for append.
62 */
63 private final boolean append;
64
65 /**
66 * The associated channel, initialized lazily.
67 */
68 private FileChannel channel;
69
70 /**
108 * A new <code>FileDescriptor</code> object is created to represent this
109 * file connection.
110 * <p>
111 * First, if there is a security manager, its <code>checkWrite</code>
112 * method is called with <code>name</code> as its argument.
113 * <p>
114 * If the file exists but is a directory rather than a regular file, does
115 * not exist but cannot be created, or cannot be opened for any other
116 * reason then a <code>FileNotFoundException</code> is thrown.
117 *
118 * @param name the system-dependent file name
119 * @param append if <code>true</code>, then bytes will be written
120 * to the end of the file rather than the beginning
121 * @exception FileNotFoundException if the file exists but is a directory
122 * rather than a regular file, does not exist but cannot
123 * be created, or cannot be opened for any other reason.
124 * @exception SecurityException if a security manager exists and its
125 * <code>checkWrite</code> method denies write access
126 * to the file.
127 * @see java.lang.SecurityManager#checkWrite(java.lang.String)
128 * @since 1.1
129 */
130 public FileOutputStream(String name, boolean append)
131 throws FileNotFoundException
132 {
133 this(name != null ? new File(name) : null, append);
134 }
135
136 /**
137 * Creates a file output stream to write to the file represented by
138 * the specified <code>File</code> object. A new
139 * <code>FileDescriptor</code> object is created to represent this
140 * file connection.
141 * <p>
142 * First, if there is a security manager, its <code>checkWrite</code>
143 * method is called with the path represented by the <code>file</code>
144 * argument as its argument.
145 * <p>
146 * If the file exists but is a directory rather than a regular file, does
147 * not exist but cannot be created, or cannot be opened for any other
148 * reason then a <code>FileNotFoundException</code> is thrown.
|