This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

B & S read method

Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In SUN interface
// Reads a record from the file. Returns an array where each
// element is a record value.
public String [] read(int recNo) throws RecordNotFoundException;

How recNo will come ??
I put recNo in Contractor Object as primary key .
I made method getContractorList which is putting each record in recordNumbers Map and getContractorList method is calling read(int recNo).

I am calling getContractorList in constructor of ContractorFileAccess.

Constructor of ContractorFileAccess
public ContractorFileAccess(String suppliedDbPath)
throws FileNotFoundException, IOException {
log.entering("ContractorFileAccess", "getContractorFileAccess",
if (database == null) {
suppliedDbPath = "C:\\Sun\\SDK\\jdk\\bin\\db-2x1.db";
database = new RandomAccessFile(suppliedDbPath, "rw");
System.out.println("database length ::::" + database.length());
dbPath = suppliedDbPath;
log.fine("database opened and file location table populated");
} else if (dbPath != suppliedDbPath) {
log.warning("Only one database location can be specified. "
+ "Current location: " + dbPath + " "
+ "Ignoring specified path: " + suppliedDbPath);
log.exiting("ContractorFileAccess", "ContractorFileAccess");

private List<String[]> getContractorList(boolean buildRecordNumbers) throws IOException {
log.entering("ContractorFileAccess", "getContractorList", buildRecordNumbers);
List<String[]> returnValue = new ArrayList<String[]>();

if (buildRecordNumbers) {

try {
for (int locationInFile = 0; locationInFile < database.length(); locationInFile += Contractor.RECORD_LENGTH) {
String[] contractorRecord = read(locationInFile);
log.fine("retrieving record at " + locationInFile);
if (contractorRecord == null) {
log.fine("found deleted record ");
} else {
log.fine("found record " + contractorRecord);
if (buildRecordNumbers) {
recordNumbers.put(contractorRecord, locationInFile);
} finally {
if (buildRecordNumbers) {

log.exiting("ContractorFileAccess", "getContractorList");
return returnValue;


public String[] read(int recNo) throws IOException{
log.entering("ContractorFileAccess", "read", recNo);
final byte[] input = new byte[Contractor.RECORD_LENGTH];

// Block other users for minimum time possible. Get exlusive access to
// the file, read the entire set of bytes, then release exclusive lock.
synchronized (database) {;

// now convert those bytes into a Contractor value object. Note that the thread
// that is doing this conversion can be running while other threads are
// doing other work - we are no longer blocking them.

* class to assist in converting from the one big byte[] into multiple
* String[] - one String per field. Refer to the book for more info.
class RecordFieldReader {
/** field to track the position within the byte array */
private int offset = 0;
* converts the required number of bytes into a String.
* @param length the length to be converted from current offset.
* @return the converted String
* @throws UnsupportedEncodingException if "UTF-8" not known.
String read(int length) throws UnsupportedEncodingException {
String str = new String(input, offset, length, "UTF-8");
offset += length;
return str.trim();

RecordFieldReader readRecord = new RecordFieldReader();
String subContractorName =;

String[] returnValue={subContractorName};

log.exiting("ContractorFileAccess", "read", returnValue);
return returnValue;


Please give your inputs that did I implemented read method of SUN interface correctly .
Ranch Hand
Posts: 123
Hibernate Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So what is your actual question?
    Bookmark Topic Watch Topic
  • New Topic