0

I want to append a line of text into a file - however, I want to get the position of the string in the file, such that I can access the string directly using a RandomAccessFile and file.seek() (or similar)

The issue is that alot of file i/o operations are asynch, and the write operations can happen within very short time intervals - suggesting a asynch write, since everything else is inefficient. How do I make sure the filepointer is calculated correctly? I am a newcomer to Java and dont yet understand the details of the different methods of File I/O, so excuse my Question if using a BufferedWriter is exactly what I am looking for, but how do you get the current length of that?

EDIT: Reading the entire file is NOT an option. The file is large and as I said, the write operations happen often, several hundred every second in peak times.

CBenni
  • 555
  • 1
  • 7
  • 20
  • Possible duplicate: http://stackoverflow.com/questions/1625234/how-to-append-text-to-an-existing-file-in-java – ccozad Jan 20 '14 at 22:00
  • @ccozad that doesnt explain how to get the position at all from what I see, unless I missed something. (I have seen that page before) – CBenni Jan 20 '14 at 22:01
  • The way I read it is that you need to append data to a file? Also you say everything else is "inefficient". Are you assuming this because someone told you Java was slow or because you have tried it and the performance did not meet your needs? The community can help fix slow code but it is hard to help when you have not tried the standard stream facilities. – ccozad Jan 21 '14 at 00:17
  • Also for the other question about Streams there is a position() and size() for classes conforming to the SeekableByteChannel interface. http://docs.oracle.com/javase/tutorial/essential/io/rafs.html and http://docs.oracle.com/javase/7/docs/api/java/nio/channels/SeekableByteChannel.html#position-- – ccozad Jan 21 '14 at 00:22
  • @ccozad since there was apparently no easy solution available, I wrote my own wrapper for BufferedWriter, that keeps track of the bytes read itself. I will post the source as an answer once I am done and have it tested. – CBenni Jan 21 '14 at 00:31
  • SeekableByteChannel Interface, size. See FileChannel http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html – ccozad Jan 21 '14 at 00:34

1 Answers1

0

Refer to the FileChannel class: http://docs.oracle.com/javase/7/docs/api/java/nio/channels/FileChannel.html

Relevant snippets from the link:

The file itself contains a variable-length sequence of bytes that can be read and written and whose current size can be queried. The size of the file increases when bytes are written beyond its current size;

...

File channels are safe for use by multiple concurrent threads. The close method may be invoked at any time, as specified by the Channel interface. Only one operation that involves the channel's position or can change its file's size may be in progress at any given time; attempts to initiate a second such operation while the first is still in progress will block until the first operation completes. Other operations, in particular those that take an explicit position, may proceed concurrently; whether they in fact do so is dependent upon the underlying implementation and is therefore unspecified.

ccozad
  • 1,119
  • 8
  • 13