![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Mar 2005
Posts: 1
Rep Power: 0
![]() |
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; } } |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|