50 |
*/ |
*/ |
51 |
public class FileOutputStream extends OutputStream |
public class FileOutputStream extends OutputStream |
52 |
{ |
{ |
|
|
|
|
/* |
|
|
* Instance Variables |
|
|
*/ |
|
|
|
|
53 |
private FileDescriptor fd; |
private FileDescriptor fd; |
54 |
|
|
|
/*************************************************************************/ |
|
|
|
|
|
/* |
|
|
* Constructors |
|
|
*/ |
|
|
|
|
55 |
/** |
/** |
56 |
* This method initializes a <code>FileOutputStream</code> object to write |
* This method initializes a <code>FileOutputStream</code> object to write |
57 |
* to the named file. The file is created if it does not exist, and |
* to the named file. The file is created if it does not exist, and |
58 |
* the bytes written are written starting at the beginning of the file. |
* the bytes written are written starting at the beginning of the file. |
59 |
* <p> |
* <p> |
60 |
* Before opening a file, a security check is performed by calling the |
* Before opening a file, a security check is performed by calling the |
61 |
* <code>checkWrite</code> method of the <code>SecurityManager</code> (if |
* <code>checkWrite</code> method of the <code>SecurityManager</code> (if |
62 |
* one exists) with the name of the file to be opened. An exception is |
* one exists) with the name of the file to be opened. An exception is |
63 |
* thrown if writing is not allowed. |
* thrown if writing is not allowed. |
64 |
* |
* |
65 |
* @param name The name of the file this stream should write to |
* @param name The name of the file this stream should write to |
66 |
* |
* |
67 |
* @exception SecurityException If write access to the file is not allowed |
* @exception SecurityException If write access to the file is not allowed |
68 |
* @exception FileNotFoundException If a non-security error occurs |
* @exception FileNotFoundException If a non-security error occurs |
69 |
*/ |
*/ |
70 |
public |
public |
71 |
FileOutputStream(String name) throws SecurityException, FileNotFoundException |
FileOutputStream(String name) throws SecurityException, FileNotFoundException |
72 |
{ |
{ |
73 |
this(name, false); |
this(name, false); |
74 |
} |
} |
75 |
|
|
|
/*************************************************************************/ |
|
|
|
|
76 |
/** |
/** |
77 |
* This method initializes a <code>FileOutputStream</code> object to write |
* This method initializes a <code>FileOutputStream</code> object to write |
78 |
* to the specified <code>File</code> object. The file is created if it |
* to the specified <code>File</code> object. The file is created if it |
79 |
* does not exist, and the bytes written are written starting at the |
* does not exist, and the bytes written are written starting at the |
80 |
* beginning of the file. |
* beginning of the file. |
81 |
* <p> |
* <p> |
82 |
* Before opening a file, a security check is performed by calling the |
* Before opening a file, a security check is performed by calling the |
83 |
* <code>checkWrite</code> method of the <code>SecurityManager</code> (if |
* <code>checkWrite</code> method of the <code>SecurityManager</code> (if |
84 |
* one exists) with the name of the file to be opened. An exception is |
* one exists) with the name of the file to be opened. An exception is |
85 |
* thrown if writing is not allowed. |
* thrown if writing is not allowed. |
86 |
* |
* |
87 |
* @param file The <code>File</code> object this stream should write to |
* @param file The <code>File</code> object this stream should write to |
88 |
* |
* |
89 |
* @exception SecurityException If write access to the file is not allowed |
* @exception SecurityException If write access to the file is not allowed |
90 |
* @exception FileNotFoundException If a non-security error occurs |
* @exception FileNotFoundException If a non-security error occurs |
91 |
*/ |
*/ |
92 |
public |
public |
93 |
FileOutputStream(File file) throws SecurityException, FileNotFoundException |
FileOutputStream(File file) throws SecurityException, FileNotFoundException |
94 |
{ |
{ |
95 |
this(file.getPath(), false); |
this(file.getPath(), false); |
96 |
} |
} |
97 |
|
|
|
/*************************************************************************/ |
|
|
|
|
98 |
/** |
/** |
99 |
* This method initializes a <code>FileOutputStream</code> object to write |
* This method initializes a <code>FileOutputStream</code> object to write |
100 |
* to the specified <code>File</code> object. The file is created if it |
* to the specified <code>File</code> object. The file is created if it |
101 |
* does not exist, and the bytes written are written starting at the |
* does not exist, and the bytes written are written starting at the |
102 |
* beginning of the file if the <code>append</code> parameter is |
* beginning of the file if the <code>append</code> parameter is |
103 |
* <code>false</code>. Otherwise bytes are written at the end of the |
* <code>false</code>. Otherwise bytes are written at the end of the |
104 |
* file. |
* file. |
105 |
* <p> |
* <p> |
106 |
* Before opening a file, a security check is performed by calling the |
* Before opening a file, a security check is performed by calling the |
107 |
* <code>checkWrite</code> method of the <code>SecurityManager</code> (if |
* <code>checkWrite</code> method of the <code>SecurityManager</code> (if |
108 |
* one exists) with the name of the file to be opened. An exception is |
* one exists) with the name of the file to be opened. An exception is |
109 |
* thrown if writing is not allowed. |
* thrown if writing is not allowed. |
110 |
* |
* |
111 |
* @param file The <code>File</code> object this stream should write to |
* @param file The <code>File</code> object this stream should write to |
112 |
* @param append <code>true</code> to append bytes to the end of the file, |
* @param append <code>true</code> to append bytes to the end of the file, |
113 |
* or <code>false</code> to write bytes to the beginning |
* or <code>false</code> to write bytes to the beginning |
114 |
* |
* |
115 |
* @exception SecurityException If write access to the file is not allowed |
* @exception SecurityException If write access to the file is not allowed |
116 |
* @exception FileNotFoundException If a non-security error occurs |
* @exception FileNotFoundException If a non-security error occurs |
117 |
*/ |
*/ |
118 |
public |
public |
119 |
FileOutputStream(File file, boolean append) throws FileNotFoundException |
FileOutputStream(File file, boolean append) throws FileNotFoundException |
120 |
{ |
{ |
121 |
this(file.getPath(), append); |
this(file.getPath(), append); |
122 |
} |
} |
123 |
|
|
|
/*************************************************************************/ |
|
|
|
|
124 |
/** |
/** |
125 |
* This method initializes a <code>FileOutputStream</code> object to write |
* This method initializes a <code>FileOutputStream</code> object to write |
126 |
* to the named file. The file is created if it does not exist, and |
* to the named file. The file is created if it does not exist, and |
127 |
* the bytes written are written starting at the beginning of the file if |
* the bytes written are written starting at the beginning of the file if |
128 |
* the <code>append</code> argument is <code>false</code> or at the end |
* the <code>append</code> argument is <code>false</code> or at the end |
129 |
* of the file if the <code>append</code> argument is true. |
* of the file if the <code>append</code> argument is true. |
130 |
* <p> |
* <p> |
131 |
* Before opening a file, a security check is performed by calling the |
* Before opening a file, a security check is performed by calling the |
132 |
* <code>checkWrite</code> method of the <code>SecurityManager</code> (if |
* <code>checkWrite</code> method of the <code>SecurityManager</code> (if |
133 |
* one exists) with the name of the file to be opened. An exception is |
* one exists) with the name of the file to be opened. An exception is |
134 |
* thrown if writing is not allowed. |
* thrown if writing is not allowed. |
135 |
* |
* |
136 |
* @param name The name of the file this stream should write to |
* @param name The name of the file this stream should write to |
137 |
* @param append <code>true</code> to append bytes to the end of the file, |
* @param append <code>true</code> to append bytes to the end of the file, |
138 |
* or <code>false</code> to write bytes to the beginning |
* or <code>false</code> to write bytes to the beginning |
139 |
* |
* |
140 |
* @exception SecurityException If write access to the file is not allowed |
* @exception SecurityException If write access to the file is not allowed |
141 |
* @exception FileNotFoundException If a non-security error occurs |
* @exception FileNotFoundException If a non-security error occurs |
142 |
*/ |
*/ |
143 |
public FileOutputStream(String name, boolean append) |
public FileOutputStream(String name, boolean append) |
144 |
throws SecurityException, FileNotFoundException |
throws SecurityException, FileNotFoundException |
145 |
{ |
{ |
162 |
} |
} |
163 |
} |
} |
164 |
|
|
|
/*************************************************************************/ |
|
|
|
|
165 |
/** |
/** |
166 |
* This method initializes a <code>FileOutputStream</code> object to write |
* This method initializes a <code>FileOutputStream</code> object to write |
167 |
* to the file represented by the specified <code>FileDescriptor</code> |
* to the file represented by the specified <code>FileDescriptor</code> |
168 |
* object. This method does not create any underlying disk file or |
* object. This method does not create any underlying disk file or |
169 |
* reposition the file pointer of the given descriptor. It assumes that |
* reposition the file pointer of the given descriptor. It assumes that |
170 |
* this descriptor is ready for writing as is. |
* this descriptor is ready for writing as is. |
171 |
* <p> |
* <p> |
172 |
* Before opening a file, a security check is performed by calling the |
* Before opening a file, a security check is performed by calling the |
173 |
* <code>checkWrite</code> method of the <code>SecurityManager</code> (if |
* <code>checkWrite</code> method of the <code>SecurityManager</code> (if |
174 |
* one exists) with the specified <code>FileDescriptor</code> as an argument. |
* one exists) with the specified <code>FileDescriptor</code> as an argument. |
175 |
* An exception is thrown if writing is not allowed. |
* An exception is thrown if writing is not allowed. |
176 |
* |
* |
177 |
* @param file The <code>FileDescriptor</code> this stream should write to |
* @param file The <code>FileDescriptor</code> this stream should write to |
178 |
* |
* |
179 |
* @exception SecurityException If write access to the file is not allowed |
* @exception SecurityException If write access to the file is not allowed |
180 |
*/ |
*/ |
181 |
public FileOutputStream(FileDescriptor fd) throws SecurityException |
public FileOutputStream(FileDescriptor fd) throws SecurityException |
182 |
{ |
{ |
183 |
// Hmm, no other exception but this one to throw, but if the descriptor |
// Hmm, no other exception but this one to throw, but if the descriptor |
192 |
this.fd = fd; |
this.fd = fd; |
193 |
} |
} |
194 |
|
|
|
/*************************************************************************/ |
|
|
|
|
195 |
/** |
/** |
196 |
* This method returns a <code>FileDescriptor</code> object representing |
* This method returns a <code>FileDescriptor</code> object representing |
197 |
* the file that is currently being written to |
* the file that is currently being written to |
198 |
* |
* |
199 |
* @return A <code>FileDescriptor</code> object for this stream |
* @return A <code>FileDescriptor</code> object for this stream |
200 |
* |
* |
201 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
202 |
*/ |
*/ |
203 |
public final FileDescriptor getFD() throws IOException |
public final FileDescriptor getFD() throws IOException |
204 |
{ |
{ |
205 |
return(fd); |
return(fd); |
206 |
} |
} |
207 |
|
|
|
/*************************************************************************/ |
|
|
|
|
208 |
/** |
/** |
209 |
* This method writes a single byte of data to the file. |
* This method writes a single byte of data to the file. |
210 |
* |
* |
211 |
* @param b The byte of data to write, passed as an <code>int</code> |
* @param b The byte of data to write, passed as an <code>int</code> |
212 |
* |
* |
213 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
214 |
*/ |
*/ |
215 |
public void write(int b) throws IOException |
public void write(int b) throws IOException |
216 |
{ |
{ |
217 |
fd.write(b); |
fd.write(b); |
218 |
} |
} |
219 |
|
|
|
/*************************************************************************/ |
|
|
|
|
220 |
/** |
/** |
221 |
* This method writes all the bytes in the specified array to the |
* This method writes all the bytes in the specified array to the |
222 |
* file. |
* file. |
223 |
* |
* |
224 |
* @param buf The array of bytes to write to the file |
* @param buf The array of bytes to write to the file |
225 |
* |
* |
226 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
227 |
*/ |
*/ |
228 |
public void write(byte[] buf) throws IOException |
public void write(byte[] buf) throws IOException |
229 |
{ |
{ |
230 |
write(buf, 0, buf.length); |
write(buf, 0, buf.length); |
231 |
} |
} |
232 |
|
|
|
/*************************************************************************/ |
|
|
|
|
233 |
/** |
/** |
234 |
* This method writes <code>len</code> bytes from the byte array |
* This method writes <code>len</code> bytes from the byte array |
235 |
* <code>buf</code> to the file starting at index <code>offset</code>. |
* <code>buf</code> to the file starting at index <code>offset</code>. |
236 |
* |
* |
237 |
* @param buf The array of bytes to write to the file |
* @param buf The array of bytes to write to the file |
238 |
* @param offset The offset into the array to start writing bytes from |
* @param offset The offset into the array to start writing bytes from |
239 |
* @param len The number of bytes to write to the file |
* @param len The number of bytes to write to the file |
240 |
* |
* |
241 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
242 |
*/ |
*/ |
243 |
public void write(byte[] buf, int offset, int len) throws IOException |
public void write(byte[] buf, int offset, int len) throws IOException |
244 |
{ |
{ |
245 |
fd.write(buf, offset, len); |
fd.write(buf, offset, len); |
246 |
} |
} |
247 |
|
|
|
/*************************************************************************/ |
|
|
|
|
248 |
/** |
/** |
249 |
* This method closes the underlying file. Any further attempts to |
* This method closes the underlying file. Any further attempts to |
250 |
* write to this stream will likely generate an exception since the |
* write to this stream will likely generate an exception since the |
251 |
* file is closed. |
* file is closed. |
252 |
* |
* |
253 |
* @exception IOException If an error occurs |
* @exception IOException If an error occurs |
254 |
*/ |
*/ |
255 |
public void close() throws IOException |
public void close() throws IOException |
256 |
{ |
{ |
257 |
fd.close(); |
fd.close(); |
258 |
} |
} |
259 |
|
|
|
/*************************************************************************/ |
|
|
|
|
260 |
/** |
/** |
261 |
* This method creates a java.nio.channels.FileChannel. |
* This method creates a java.nio.channels.FileChannel. |
262 |
* Nio does not allow one to create a file channel directly. |
* Nio does not allow one to create a file channel directly. |