• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wierd effects with File Channel write

 
James Giraldo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First time post from this forum after being a silent observer for a couple months now while working on my URLyBird assignment. I'm hoping someone can help me with some strangeness I'm having using the absolute write method of FileChannel. This is for the create functionality of the assignment.
I've got no problem reading the data base file into a ByteBuffer using a FileChannel. In one case, I'm using a FileOutputStream to get the cannel.
In another I'm using a RandomAccessFile.
For the FileOutputStream case, when it comes to writing to the file via the FileChannel using channel.write(ByteBuffer src, long position) the entire contents of the file are deleted. This also occurs when I set the channel's position apart from the write().
One thing that has me baffled, I am able to write the entire modified ByteBuffer to the file using channel.write(modifiedBuffer), just not the record byte buffer at a particular location in the file.
And for the RandomAccessFile channel, the same channel.write(buffer, position) results in no change at all on the file.
I can see that I might have the size and content of the actual recordByteBuffer incorrect, but I beleive I should be able to at least write something to file.
Essentially, here's my code:
Appreciate any help.
.
.
.
recordByteBuffer.rewind();
recordByteBuffer.flip();
.
.
Charset charset = Charset.forName(STRING_ENCODING);
charset.decode(recordByteBuffer);
.
.
fileOutputStream = new FileOutputStream(dbPath);
writerChannel = fileOutputStream.getChannel();
.
.
channel.write(recordByteBuffer, recordOffset);
channel.force(false);
channel.close();
Thanks again for any assistence on this.
James
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the API for new FileOutputStream(String) and compare it to new FileOutputStream(String, boolean).
 
James Giraldo
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried creating the FileOutputStream with the false parameter and this also results in an empty file.
Creating the FileOutputStream with true makes no update to the file.
The API says the bytes will be written to the end of the file rather than the beginning if using true. If using false I assume the bytes will be written at the beginning, but the API doesn't specifically say. The single arg constructor for FileOutputStream also does not mentions anything about how it defaults.
I'm wondering if I need something other than being able to specify writing bytes at the beginning or ending, since I'd also need to be able to write to a position somewhere in the middle of the file.
Any other ideas?
Thanks
James
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic