Package repast.simphony.integration
Class RandomAccessWriter
java.lang.Object
java.io.Writer
java.io.PrintWriter
repast.simphony.integration.RandomAccessWriter
- All Implemented Interfaces:
Closeable
,Flushable
,Appendable
,AutoCloseable
A
PrintWriter
that writes to a file channel, allowing it to do random access
types of writes. This supports a stack of marks (and therefore resets). It also allows for
jumping to offsets in the file, determining the current file offset, and truncating the file.- Author:
- Jerry Vos
-
Field Summary
Fields inherited from class java.io.PrintWriter
out
-
Constructor Summary
ConstructorDescriptionRandomAccessWriter
(File file) Opens this writer on the specified file.RandomAccessWriter
(String fileName) Opens this writer on the specified file. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
long
Retrieves the actual offset of the file, disregarding any buffering.void
mark()
Adds a mark to the mark queue at the current location in the file.popMark()
Removes a mark from the mark queue and returns it.void
position
(long position) Positions the file to the specified position.void
reset()
This jumps the file offset back to the previously marked position.truncate
(long size) Truncates the file to the specified size.Truncates the file to the current offset.Methods inherited from class java.io.PrintWriter
append, append, append, checkError, clearError, flush, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, println, setError, write, write, write, write, write
Methods inherited from class java.io.Writer
nullWriter
-
Constructor Details
-
RandomAccessWriter
Opens this writer on the specified file.- Parameters:
file
- the file to write to- Throws:
FileNotFoundException
-
RandomAccessWriter
Opens this writer on the specified file.- Parameters:
fileName
- the name of the file to open- Throws:
FileNotFoundException
-
-
Method Details
-
getTrueOffset
Retrieves the actual offset of the file, disregarding any buffering. A result of this call is that all buffered data is flushed before this function returns.- Returns:
- the current offset of the file
- Throws:
IOException
-
mark
Adds a mark to the mark queue at the current location in the file. Since this usedgetTrueOffset()
, this causes the underlying data stream to flush.- Throws:
IOException
-
popMark
Removes a mark from the mark queue and returns it.- Returns:
- the top of the mark queue
-
reset
This jumps the file offset back to the previously marked position. This does not check if the mark stack is empty, so when calling this with an empty mark stack, an exception will be thrown. This does not pop the mark that was used off the stack.- Throws:
IOException
-
position
Positions the file to the specified position.- Parameters:
position
- the position to set the write mark to- Throws:
IOException
-
truncateToPosition
Truncates the file to the current offset. This is the same as usingtruncate(long)
with the value fromgetTrueOffset()
.- Returns:
- the truncated channel
- Throws:
IOException
-
truncate
Truncates the file to the specified size.- Parameters:
size
- the size to truncate the file to- Returns:
- the truncated channel
- Throws:
IOException
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
- Overrides:
close
in classPrintWriter
-