• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Random Access Problem

 
Franco Baresi
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I designed an app that writes data to a dat file using a record class my problem is that a funtion I used in the last line of code in the class record is setting the record size and as a result the program works fine but only reads back the first record from the dat file.

Is there any way around having to set a record size using my code. Please I need help quick I am on a deadline.

Heres the entire code you wont need all of it cause the problem is pretty straightforward

THE CLASS FILE
import java.io.*;
public class Record {
private int customer;
private String lastName;
private String firstName;
private String streetAddress;
private String townAddress;
private String countyAddress;

// Read a record from the specified RandomAccessFile
public void read( RandomAccessFile file ) throws IOException
{
customer = file.readInt();

char first[] = new char[ 15 ];

for ( int i = 0; i < first.length; i++ )
first[ i ] = file.readChar();

firstName = new String( first );

char last[] = new char[ 15 ];

for ( int i = 0; i < last.length; i++ )
last[ i ] = file.readChar();

lastName = new String( last );

char street[] = new char[ 15 ];

for ( int i = 0; i < street.length; i++ )
street[ i ] = file.readChar();

streetAddress = new String( street );

char town[] = new char[ 15 ];

for ( int i = 0; i < town.length; i++ )
town[ i ] = file.readChar();

townAddress = new String( town );

char county[] = new char[ 15 ];

for ( int i = 0; i < county.length; i++ )
county[ i ] = file.readChar();

countyAddress = new String( county );


}

// Write a record to the specified RandomAccessFile
public void write( RandomAccessFile file ) throws IOException
{
StringBuffer buf;

file.writeInt( customer );

if ( firstName != null )
buf = new StringBuffer( firstName );
else
buf = new StringBuffer( 15 );

buf.setLength( 15 );

file.writeChars( buf.toString() );

if ( lastName != null )
buf = new StringBuffer( lastName );
else
buf = new StringBuffer( 15 );

buf.setLength( 15 );

file.writeChars( buf.toString() );

if ( streetAddress != null )
buf = new StringBuffer( streetAddress );
else
buf = new StringBuffer( 15 );

buf.setLength( 15 );


file.writeChars( buf.toString() );

if ( townAddress != null )
buf = new StringBuffer( townAddress );
else
buf = new StringBuffer( 15 );

buf.setLength( 15 );


file.writeChars( buf.toString() );

if ( countyAddress != null )
buf = new StringBuffer( countyAddress );
else
buf = new StringBuffer( 15 );

buf.setLength( 15 );

file.writeChars( buf.toString() );

}

public void setCustomer( int a ) { customer = a; }

public int getCustomer() { return customer; }

public void setFirstName( String f ) { firstName = f; }

public String getFirstName() { return firstName; }

public void setLastName( String l ) { lastName = l; }

public String getLastName() { return lastName; }

public void setStreetAddress( String s ) { streetAddress = s; }

public String getStreetAddress() { return streetAddress; }

public void setTownAddress( String t ) { townAddress = t; }

public String getTownAddress() { return townAddress; }

public void setCountyAddress( String c ) { countyAddress = c; }

public String getCountyAddress() { return countyAddress; }

// NOTE: This method contains a hard coded value for the
// size of a record of information.
public static int size() { return 135; }
}

The WRITE CODE
import java.io.*;
import java.awt.*;
import java.awt.event.*;

public class WriteRandomFile extends Frame
implements ActionListener {

// TextFields where user enters customer number, first name etc
private TextField customerField, firstNameField,
lastNameField, streetAddressField, townAddressField, countyAddressField;

private Button enter, // send record to file
done; // quit program

// Application other pieces
private RandomAccessFile output; // file for output
private Record data;

// Constructor -- intialize the Frame
public WriteRandomFile()
{
super( "Enter a new Customer" );

data = new Record();

// Open the file
try {
output = new RandomAccessFile( "customers.dat", "rw" );
}
catch ( IOException e ) {
System.err.println( e.toString() );
System.exit( 1 );
}

setSize( 300, 150 );
setLayout( new GridLayout(7,2));

// create the components of the Frame
add( new Label( "Customer Number" ) );
customerField = new TextField();
add( customerField );

add( new Label( "First Name" ) );
firstNameField = new TextField( 20 );
add( firstNameField );

add( new Label( "Last Name" ) );
lastNameField = new TextField( 20 );
add( lastNameField );

add( new Label( "Steet Address" ) );
streetAddressField = new TextField( 20 );
add( streetAddressField );

add( new Label( "Town Address" ) );
townAddressField = new TextField( 20 );
add( townAddressField );

add( new Label( "County Address" ) );
countyAddressField = new TextField( 20 );
add( countyAddressField );

enter = new Button( "Enter" );
enter.addActionListener( this );
add( enter );

done = new Button( "Done" );
done.addActionListener( this );
add( done );

setVisible( true );
}

public void addRecord()
{
int customerNumber = 0;
Double d;

if ( ! customerField.getText().equals( "" ) ) {

// output the values to the file
try {
customerNumber =
Integer.parseInt( customerField.getText() );

if ( customerNumber > 0 && customerNumber <= 100 ) {
data.setCustomer( customerNumber );
data.setFirstName( firstNameField.getText() );
data.setLastName( lastNameField.getText() );
data.setStreetAddress( streetAddressField.getText() );
data.setTownAddress( townAddressField.getText() );
data.setCountyAddress( countyAddressField.getText() );



output.seek(
(long) ( customerNumber-1 ) * Record.size() );
data.write( output );
}

// clear the TextFields
customerField.setText( "" );
firstNameField.setText( "" );
lastNameField.setText( "" );
streetAddressField.setText( "" );
townAddressField.setText( "" );
countyAddressField.setText( "" );
}
catch ( NumberFormatException nfe ) {
System.err.println(
"You must enter an integer customer number" );
}
catch ( IOException io ) {
System.err.println(
"Error during write to file\n" +
io.toString() );
System.exit( 1 );
}
}
}

public void actionPerformed( ActionEvent e )
{
addRecord();

if ( e.getSource() == done ) {
try {
output.close();
}
catch ( IOException io ) {
System.err.println( "File not closed properly\n" +
io.toString() );
}

System.exit( 0 );
}
}

// Instantiate a WriteRandomFile object and start the program
public static void main( String args[] )
{
new WriteRandomFile();
}
}



THE READ CODE
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.text.DecimalFormat;

public class ReadRandomFile extends Frame
implements ActionListener {

// TextFields to display customer number, first name,
// last name and balance.
private TextField customerField, firstNameField,
lastNameField, streetAddressField, townAddressField, countyAddressField;
private Button next, // get next record in file
done; // quit program

// Application other pieces
private RandomAccessFile input;
private Record data;

// Constructor -- initialize the Frame
public ReadRandomFile()
{
super( "View Customers Information" );

// Open the file
try {
input = new RandomAccessFile( "customers.dat", "r" );
}
catch ( IOException e ) {
System.err.println( e.toString() );
System.exit( 1 );
}

data = new Record();

setSize( 300, 150 );
setLayout( new GridLayout(7,2));

// create the components of the Frame
add( new Label( "Customer Number" ) );
customerField = new TextField();
customerField.setEditable( false );
add( customerField );

add( new Label( "First Name" ) );
firstNameField = new TextField( 20 );
firstNameField.setEditable( false );
add( firstNameField );

add( new Label( "Last Name" ) );
lastNameField = new TextField( 20 );
lastNameField.setEditable( false );
add( lastNameField );

add( new Label( "Steet Address" ) );
streetAddressField = new TextField( 20 );
streetAddressField.setEditable( false );
add( streetAddressField );

add( new Label( "Town Address" ) );
townAddressField = new TextField( 20 );
townAddressField.setEditable( false );
add( townAddressField );

add( new Label( "County Address" ) );
countyAddressField = new TextField( 20 );
countyAddressField.setEditable( false );
add( countyAddressField );

next = new Button( "Next" );
next.addActionListener( this );
add( next );

done = new Button( "Done" );
done.addActionListener( this );
add( done );


setVisible( true );
}

public void actionPerformed( ActionEvent e )
{
if ( e.getSource() == next )
readRecord();
else
closeFile();
}

public void readRecord()
{


// read a record and display
try {
do {
data.read( input );
} while ( data.getCustomer() == 0 );

customerField.setText(
String.valueOf( data.getCustomer() ) );
firstNameField.setText( data.getFirstName() );
lastNameField.setText( data.getLastName() );
streetAddressField.setText( data.getStreetAddress() );
townAddressField.setText( data.getTownAddress() );
countyAddressField.setText( data.getCountyAddress() );

}
catch ( EOFException eof ) {
closeFile();
}
catch ( IOException e ) {
System.err.println( "Error during read from file\n" +
e.toString() );
System.exit( 1 );
}
}

private void closeFile()
{
try {
input.close();
System.exit( 0 );
}
catch ( IOException e ) {
System.err.println( "Error closing file\n" +
e.toString() );
System.exit( 1 );
}
}

// Instantiate a ReadRandomFile object and start the program
public static void main( String args[] )
{
new ReadRandomFile();
}
}
 
Joe Ess
Bartender
Pie
Posts: 9266
10
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the JavaRanch, Franco.
Originally posted by Franco Baresi:

Is there any way around having to set a record size using my code.


No.
You should read the JavaRanch FAQ entry on How to ask questions the Smart Way, especially the Tell The Details section.
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Franco,

Welcome to JavaRanch!

Please don't post the same question to more than one forum. I deleted the other copy you posted in the Intermediate forum.
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I skimmed the code and saw one thing that looked suspect, but I don't know exactly how it works. If you have a first name less than 15 characters, you call setLength() on the StringBuffer to pad it with null characters ('\u0000'). But when you call toString() on the buffer, does it put those nulls into the String or strip them off?

Anyway, post the actual problem you're having, what happens, exceptions, whatever. Then we might be able to help you.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic