• Post Reply Bookmark Topic Watch Topic
  • New Topic

What approach should be followed?  RSS feed

 
Manoj Gundawar
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have a problem which has to deal with a file. The file has header information (about length of record etc) and then actual records. The header information is written using dataoutputstream. But the actual records after that are in the character (ASCI) format.
Now I have to do various operations on this file like:
1. Add record (at the end)
2. Delete record (from anywhere )
3. Search a particular record
5. Modify any field of any given record.
4. Lock a particular record.
I tried with RandomAccessFiles. Searching and adding the record is working fine. But I am not sure if I can lock a particular record if I use this randomaccessfile.
Also I am doing manipulations at byte level. (reading one record at a time, and each field in a separate array and then working on each array depending upon the requirement)
Is there any other/better way to do this? I guess I am not fully utilizing the java’s foundation classes.
Will this cause a problem if I have to make this service available at the server and use the RMI architecture to allow clients to do those operations?
I am quite confused as there are so many ways of dealing with file in JAVA. IS NIO is the best way to do this?
Detail answer is much appreciated.
Thanks in advance.
Manoj
 
Ta Ri Ki Sun
Ranch Hand
Posts: 442
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jmannu gundawar:
Hi,
I have a problem which has to deal with a file. The file has header information (about length of record etc) and then actual records. The header information is written using dataoutputstream. But the actual records after that are in the character (ASCI) format.
Now I have to do various operations on this file like:
1. Add record (at the end)
2. Delete record (from anywhere )
3. Search a particular record
5. Modify any field of any given record.
4. Lock a particular record.
I tried with RandomAccessFiles. Searching and adding the record is working fine. But I am not sure if I can lock a particular record if I use this randomaccessfile.
Also I am doing manipulations at byte level. (reading one record at a time, and each field in a separate array and then working on each array depending upon the requirement)
Is there any other/better way to do this? I guess I am not fully utilizing the java’s foundation classes.
Will this cause a problem if I have to make this service available at the server and use the RMI architecture to allow clients to do those operations?
I am quite confused as there are so many ways of dealing with file in JAVA. IS NIO is the best way to do this?
Detail answer is much appreciated.
Thanks in advance.
Manoj


Hi Manoj, I assume you're working on the SCJD assignment, certainly looks like it, if so then maybe you'll get more feedback in the SCJD forum.
anyway, for starters, dont try locking records with RandomAccessFile or any other File object for that matter, NIO provides some locking facilities but dont consider those either, except maybe the entire file as an optional precaution, but since its OS dependent it cant be guarenteed , hence the optional precaution.
anyhat, for locking consider that all your records start at a specific position in the file, so maintain a collection of those positions or record numbers assigned to those positions and treat the positions themselves as locks, so if recNumber 5 is locked and is in position 2314 then dont allow any modifications to that record.
make sense?
 
Manoj Gundawar
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Ta ra ki sung.
You are right. This is for SCJD exam. I will probably post this query in that forum.
thanks
manoj
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!