public class RandomAccessFileOutputStream
extends java.io.OutputStream
Constructor and Description |
---|
RandomAccessFileOutputStream(java.io.File file,
java.lang.String mode)
Constructor with file.
|
RandomAccessFileOutputStream(RandomAccessFileInputStream in)
Constructor with input stream.
|
RandomAccessFileOutputStream(RandomAccessFileOutputStream out)
Constructor with output stream.
|
Modifier and Type | Method and Description |
---|---|
void |
close()
Closes the underlying file and sets the length to the current position.
|
void |
flush()
Calls
sync() of the underlying file's descriptor. |
java.nio.channels.FileChannel |
getChannel()
Returns the channel used by the random access file.
|
long |
getCounter()
Returns the number of bytes written since start or since last call to
resetCounter() . |
java.nio.channels.FileLock |
lock(int tryLockMax,
long tryWaitMillis)
Lock the file.
|
void |
resetCounter()
Sets the internal counter to
0 . |
void |
seek(long pos)
Sets the file-pointer offset, measured from the beginning of this file,
at which the next read or write occurs.
|
void |
setLength(long newLength)
Sets the length of this file.
|
void |
truncate()
Sets the length of the file to the number of written bytes.
|
void |
write(byte[] b)
Writes
b.length bytes from the specified byte array to this
file, starting at the current file pointer. |
void |
write(byte[] b,
int off,
int len)
Writes
len bytes from the specified byte array starting at
offset off to the file. |
void |
write(int b)
Writes the specified byte to this file.
|
public RandomAccessFileOutputStream(java.io.File file, java.lang.String mode) throws java.io.FileNotFoundException
file
- File the stream operates on - Cannot be null
.mode
- Access mode, as described in RandomAccessFile
-
Cannot be null
.java.io.FileNotFoundException
- If the mode is "r" but the given file object does not denote
an existing regular file, or if the mode begins with "rw" but
the given file object does not denote an existing, writable
regular file and a new regular file of that name cannot be
created, or if some other error occurs while opening or
creating the file.public RandomAccessFileOutputStream(RandomAccessFileOutputStream out)
RandomAccessFile
with the argument. Be aware that closing
this stream will also close the file used by the argument!out
- The RandomAccessFile
instance from this argument
will be used - Cannot be null
.public RandomAccessFileOutputStream(RandomAccessFileInputStream in)
RandomAccessFile
with the argument. Be aware that closing
this stream will also close the file used by the argument!in
- The RandomAccessFile
instance from this argument
will be used - Cannot be null
.public final void write(int b) throws java.io.IOException
write
in class java.io.OutputStream
b
- the byte
to be written.java.io.IOException
- if an I/O error occurs.public final void write(byte[] b) throws java.io.IOException
b.length
bytes from the specified byte array to this
file, starting at the current file pointer.write
in class java.io.OutputStream
b
- the data.java.io.IOException
- if an I/O error occurs.public final void write(byte[] b, int off, int len) throws java.io.IOException
len
bytes from the specified byte array starting at
offset off
to the file.write
in class java.io.OutputStream
b
- the data.off
- the start offset in the data.len
- the number of bytes to write.java.io.IOException
- If an I/O error occurs.public final void flush() throws java.io.IOException
sync()
of the underlying file's descriptor.flush
in interface java.io.Flushable
flush
in class java.io.OutputStream
java.io.IOException
- If an I/O error occurs.public final void close() throws java.io.IOException
close
in interface java.io.Closeable
close
in interface java.lang.AutoCloseable
close
in class java.io.OutputStream
java.io.IOException
- If an I/O error occurs.public final java.nio.channels.FileChannel getChannel()
public final void seek(long pos) throws java.io.IOException
pos
- the offset position, measured in bytes from the beginning of
the file, at which to set the file pointer.java.io.IOException
- if pos
is less than 0
or if an
I/O error occurs.public final void setLength(long newLength) throws java.io.IOException
newLength
- The desired length of the filejava.io.IOException
- If an I/O error occurspublic final void resetCounter()
0
.public final long getCounter()
resetCounter()
.public final void truncate() throws java.io.IOException
setLength(getCounter())
.java.io.IOException
- Error setting the file length.public final java.nio.channels.FileLock lock(int tryLockMax, long tryWaitMillis) throws LockingFailedException
tryLockMax
- Number of tries to lock before throwing an exception.tryWaitMillis
- Milliseconds to sleep between retries.LockingFailedException
- Locking the file failed.