Sign in to follow this  
marcmassacre

[java] I/O Random Access

Recommended Posts

marcmassacre    106
I'm writing two separate files, one for reading and one for writing random access. When I write, it compiles, the file is created, and there is some info in it. When I read, it compiles, but nothing is read!! Here is my code(ignore the comments). Also, if anyone knows a good site that helps with this, please link, thanks.
import java.io.EOFException;
import java.io.IOException;
import java.io.RandomAccessFile;

import classes.files.RandomAccessToolRecord;

public class ReadRandomFile
{
   private RandomAccessFile input;

   // enable user to select file to open
   public void openFile()
   {
      try // open file
      {
         input = new RandomAccessFile( "tools.dat", "r" );
      } // end try
      catch ( IOException ioException )
      {
         System.err.println( "File does not exist." );
      } // end catch
   } // end method openFile
   
   // read and display records
   public void readRecords()
   {
      RandomAccessToolRecord record = new RandomAccessToolRecord();

      System.out.printf( "%-10s%-15s%s%10s\n", "Record",
         "Tool Name", "Quantity", "Cost" );

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

            // display record contents
            System.out.printf( "%-10d%-12s %d %f\n",
               record.getRecord(), record.getName(),
               record.getQuantity(), record.getCost() );
         } // end while
      } // end try
      catch ( EOFException eofException ) // close file
      {
         return; // end of file was reached
      } // end catch
      catch ( IOException ioException )
      {
         System.err.println( "Error reading file." );
         System.exit( 1 );
      } // end catch
   } // end method readRecords
   
   // close file and terminate application
   public void closeFile() 
   {
      try // close file and exit
      {
         if ( input != null )
            input.close();
      } // end try
      catch ( IOException ioException )
      {
         System.err.println( "Error closing file." );
         System.exit( 1 );
      } // end catch
   } // end method closeFile
} // end class ReadRandomFile

Share this post


Link to post
Share on other sites
marcmassacre    106
RandomAccessToolRecord


package classes.files; // packaged for reuse

import java.io.RandomAccessFile;
import java.io.IOException;

public class RandomAccessToolRecord extends ToolRecord
{
public static final int SIZE = 72;

// no-argument constructor calls other constructor with default values
public RandomAccessToolRecord()
{
this( 0, "", 0, 0.0 );
} // end no-argument RandomAccessToolRecord constructor

// initialize a RandomAccessAccountRecord
public RandomAccessToolRecord( int recordNum, String Name,
int quan, double cost )
{
super( recordNum, Name, quan, cost);
} // end four-argument RandomAccessToolRecord constructor

// read a record from specified RandomAccessFile
public void read( RandomAccessFile file ) throws IOException
{
setRecord( file.readInt() );
setName( readName( file ) );
setQuantity( file.readInt() );
setCost( file.readDouble() );
} // end method read

// ensure that name is proper length
private String readName( RandomAccessFile file ) throws IOException
{
char name[] = new char[ 15 ], temp;

for ( int count = 0; count < name.length; count++ )
{
temp = file.readChar();
name[ count ] = temp;
} // end for

return new String( name ).replace( '\0', ' ' );
} // end method readName

// write a record to specified RandomAccessFile
public void write( RandomAccessFile file ) throws IOException
{
file.writeInt( getRecord() );
writeName( file, getName() );
file.writeInt(getQuantity() );
file.writeDouble( getCost() );
} // end method write

// write a name to file; maximum of 15 characters
private void writeName( RandomAccessFile file, String name )
throws IOException
{
StringBuffer buffer = null;

if ( name != null )
buffer = new StringBuffer( name );
else
buffer = new StringBuffer( 15 );

buffer.setLength( 15 );
file.writeChars( buffer.toString() );
} // end method writeName
} // end class RandomAccessToolRecord


Thanks for your help :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this