Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

B&S: Data class

 
Christiaan Lombard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I'm having some problems with writing the class that is supposed to be reading and writing, from and to the data file and would like some pointers.
The reading part I understand, but when writing I found some problems:

When you create a FileOutputStream (append = false) then the file is emptied, which deletes all your records. So I'm guessing you are supposed to append new and updated records to the end of the file...?
If this is correct, how do you go and change the 'deleted record' flags that is further back in the file?

A solution would be to read all the records beforehand, keep it in memory for making changes and then just save it in an updated file when the server closes. Is this how it is usually done? It sounds a bit risky.

Thanks
 
Christiaan Lombard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wait... just start reading up on more advanced I/O:

Should I be using ByteBuffer, FileChannel,etc.?
 
Karthik Shiraly
Bartender
Posts: 1210
25
Android C++ Java Linux PHP Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, Have you considered java.io.RandomAccessFile?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christiaan,

The RandomAccessFile is the one you need (as Karthik already pointed out).

Kind regards,
Roel
 
Christiaan Lombard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aaahhh. That makes it a lot simpler. Thanks a lot.
 
Dennis Grimbergen
Ranch Hand
Posts: 159
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that using classes from the NIO packages is not allowed.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy, Christiaan!

Champion, please take a look at this simple tool, which reads the .db file and prints its content on the console. I think it might be helpful!
 
Christiaan Lombard
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Roberto. What are the pros and cons of using your getValue(byte[]) instead of the methods of DataInput?

Also, I ran into another problem regarding exceptions. The Data class implements DBAccess(the interface that came with the assignment), so you you are not allowed to change the thrown exceptions. The only exception handled by these methods is RecordNotFoundException. Now what if you have to handle problems like corrupted data or IOExceptions? What if you are reading a record but you realize that the flag is not in the correct format? In these cases, would it be appropriate to throw RuntimeExceptions?



 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Christiaan,

You could indeed throw a RuntimeException, like DBException or DatabaseException.

Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic