LazyFileOutputStreampublic class LazyFileOutputStream extends OutputStream Class that delays opening the output file until the first bytes
shall be written or the method {@link #open open} has been invoked
explicitly. |
Fields Summary |
---|
private FileOutputStream | fos | private File | file | private boolean | append | private boolean | alwaysCreate | private boolean | opened | private boolean | closed |
Constructors Summary |
---|
public LazyFileOutputStream(String name)Creates a stream that will eventually write to the file with
the given name and replace it.
this(name, false);
| public LazyFileOutputStream(String name, boolean append)Creates a stream that will eventually write to the file with
the given name and optionally append to instead of replacing
it.
this(new File(name), append);
| public LazyFileOutputStream(File f)Creates a stream that will eventually write to the file with
the given name and replace it.
this(f, false);
| public LazyFileOutputStream(File file, boolean append)Creates a stream that will eventually write to the file with
the given name and optionally append to instead of replacing
it.
this(file, append, false);
| public LazyFileOutputStream(File file, boolean append, boolean alwaysCreate)Creates a stream that will eventually write to the file with
the given name, optionally append to instead of replacing
it, and optionally always create a file (even if zero length).
this.file = file;
this.append = append;
this.alwaysCreate = alwaysCreate;
|
Methods Summary |
---|
public synchronized void | close()Close the file.
if (alwaysCreate && !closed) {
ensureOpened();
}
if (opened) {
fos.close();
}
closed = true;
| private synchronized void | ensureOpened()
if (closed) {
throw new IOException(file + " has already been closed.");
}
if (!opened) {
fos = new FileOutputStream(file.getAbsolutePath(), append);
opened = true;
}
| public void | open()Explicitly open the file for writing.
Returns silently if the file has already been opened.
ensureOpened();
| public synchronized void | write(int b)Write a byte.
ensureOpened();
fos.write(b);
| public void | write(byte[] b)Delegates to the three-arg version.
write(b, 0, b.length);
| public synchronized void | write(byte[] b, int offset, int len)Write part of a byte array.
ensureOpened();
fos.write(b, offset, len);
|
|