Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Mar 8th, 2005, 3:44 AM   #1
oncampus
Newbie
 
Join Date: Mar 2005
Posts: 1
Rep Power: 0 oncampus is on a distinguished road
Random Access Files In JAVA

I have used a book class followed by a write class to write book information sequentially to a DAT file.

I want to read the book price data out of this dat file for an order form designed with an order class and a order write class.

I was wondering what is the best and easiest way of doing this?

Here is my book class which sets up a record of set size for each book

public class Book {
private int bookNumber;
private String productName;
private String categoryBook;
private String descriptionBook;
private String publisherBook;
private double priceBook;
// Read a record from the specified RandomAccessFile
public void read( RandomAccessFile file ) throws IOException
{
bookNumber = file.readInt();
char product[] = new char[ 15 ];
for ( int i = 0; i < product.length; i++ )
product[ i ] = file.readChar();
productName = new String( product );
char category[] = new char[ 15 ];
for ( int i = 0; i < category.length; i++ )
category[ i ] = file.readChar();
categoryBook = new String( category );
char description[] = new char[ 25 ];
for ( int i = 0; i < description.length; i++ )
description[ i ] = file.readChar();
descriptionBook = new String( description );
char publisher[] = new char[ 15 ];
for ( int i = 0; i < publisher.length; i++ )
publisher[ i ] = file.readChar();
publisherBook = new String( publisher );
priceBook = file.readDouble();
}
// Write a record to the specified RandomAccessFile
public void write( RandomAccessFile file ) throws IOException
{
StringBuffer buf;
file.writeInt( bookNumber );
if ( productName != null )
buf = new StringBuffer( productName );
else
buf = new StringBuffer( 15 );
buf.setLength( 15 );
file.writeChars( buf.toString() );
if ( categoryBook != null )
buf = new StringBuffer( categoryBook );
else
buf = new StringBuffer( 15 );
buf.setLength( 15 );
file.writeChars( buf.toString() );
if ( descriptionBook != null )
buf = new StringBuffer( descriptionBook );
else
buf = new StringBuffer( 15 );
buf.setLength( 15 );

file.writeChars( buf.toString() );
if ( publisherBook != null )
buf = new StringBuffer( publisherBook );
else
buf = new StringBuffer( 15 );
buf.setLength( 15 );
file.writeChars( buf.toString() );
file.writeDouble( priceBook );
}
public void setBookNumber( int b ) { bookNumber = b; }
public int getBook() { return bookNumber; }
public void setProductName( String n ) { productName = n; }
public String getProductName() { return productName; }
public void setCategoryBook( String c ) { categoryBook = c; }
public String getCategoryBook() { return categoryBook; }
public void setDescriptionBook( String d ) { descriptionBook = d; }
public String getDescriptionBook() { return descriptionBook; }
public void setPublisherBook( String u ) { publisherBook = u; }
public String getPublisherBook() { return publisherBook; }
public void setPriceBook( double p ) { priceBook = p; }
public double getPriceBook() { return priceBook; }
// This method contains a hard coded value for the
// size of a record of information.
public static int size() { return 132; }
}
oncampus is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:19 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC