• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

NX: RandomFileAccess question

 
Bartolo Almeda
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm using a RandomAccessFile(RAF) with 'rw' access to have my information. The RAF only have one file pointer so my question, if I only need to lock when writing how do I write and read at the same if two different places of the file with just one file pointer.
If this is a problem:
1)does the nio api solve this issue somehow? For example each Thread creates its own buffer of the file, lock that section, and make changes.
 
Andrew Monkhouse
author and jackaroo
Marshal Commander
Pie
Posts: 12014
220
C++ Firefox Browser IntelliJ IDE Java Mac Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bartolo,
Are your methods that read and write synchronized? If so, then there should be no problem - at the start of each method you seek to the record you want and then do your read or write. The JVM will treat the actions as atomic, so you will not be repositioned in the middle of your action.
Regards, Andrew
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another possibility is using NIO Channels - I'm currently considering using the thread safe I/O methods to do the writing to the RandomAccessFile.
 
Thomas Kijftenbelt
Ranch Hand
Posts: 73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Is there anybody who used NIO for the SCJD assignment?
What would be the advantages of using NIO instead of (or actually on top of) RandomAccessFile.
Greetings,
TK
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Thomas Kijftenbelt:
Hi,
Is there anybody who used NIO for the SCJD assignment?
What would be the advantages of using NIO instead of (or actually on top of) RandomAccessFile.
Greetings,
TK

I'm using NIO so I dont need to worry about the files pointers except on startup. when I assign record numbers, I wont implement create/delete so I really dont worry about the position in the file after that.
When I read a record I use the file pointer mapped to the record number and then get a channel to read everything into buffer from that position up to the length of the record.
Now I dont have to worry about one client shifting the file pointer while another was also dependant on the file pointer because nothing depends on it.
there are a great many other benefits, but thats the main one for the purppose of my assignment
to read a bit more about NIO heres 2 articles I found useful...
performance
top ten cool features of 1.4
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic