random access file!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • khajeddin
    New Member
    • Nov 2006
    • 51

    random access file!

    hi: how can get various size to a random access file progarm .
    i mean i have read that we should give a costant size tu constructor ,is it ture?
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by khajeddin
    hi: how can get various size to a random access file progarm .
    i mean i have read that we should give a costant size tu constructor ,is it ture?
    I apologize, I don't understand what you're trying to say. Random access files
    are files that you can read from or write to just like ordinary streams and on top
    of that you can position a file pointer telling the random access file where to
    read from or from where to write to. Care to rephrase your question? thanks.

    kind regards,

    Jos

    Comment

    • khajeddin
      New Member
      • Nov 2006
      • 51

      #3
      well, i'v read some where that for creating a random access file we should spesify an exact size for every record which we want to keep.
      for example if we want to create a file which can take 100 record and every record will occure 32 byte (at maximom size),we should give this size(32 byte) to the program.it means after finishing the program we can't have a record with size of 42 byte.
      now my question is : is it any way that we can make random access file which make it posible to write records with different size of byte,without risrict the program foe a maximom size?
      here is an example of a program with ristrict size of records:
      Code:
       4
       5  import java.io.RandomAccessFile;
       6  import java.io.IOException;     
       7
       8  public class RandomAccessAccountRecord extends AccountRecord
       9  {
      10      public static final int SIZE = 72;//here a size of records is spesified
      11
      12     // no-argument constructor calls other constructor with default values
      13     public RandomAccessAccountRecord()
      14     {
      15        this ( 0, "", "", 0.0 );
      16     } // end no-argument RandomAccessAccountRecord constructor
      17
      18     // initialize a RandomAccessAccountRecord
      19     public RandomAccessAccountRecord( int account, String firstName,
      20        String lastName, double balance )
      21     {
      22        super( account, firstName, lastName, balance );
      23     } // end four-argument RandomAccessAccountRecord constructor
      24
      25     // read a record from specified RandomAccessFile
      26     public void read( RandomAccessFile file ) throws IOException
      27     {
      28        setAccount( file.readInt() );
      29        setFirstName( readName( file ) );
      30        setLastName( readName( file ) );
      31        setBalance( file.readDouble() );
      32     } // end method read
      33
      34     // ensure that name is proper length
      35     private String readName( RandomAccessFile file ) throws IOException
      36     {
      37        char name[] = new char[ 15 ], temp;
      38
      39        for ( int count = 0; count < name.length; count++ )
      40        {
      41           temp = file.readChar();
      42           name[ count ] = temp;
      43        } // end for
      44
      45        return new String( name ).replace( '\0', ' ' );
      46     } // end method readName
      47
      48     // write a record to specified RandomAccessFile
      49     public void write( RandomAccessFile file ) throws IOException
      50     {
      51        file.writeInt( getAccount() );
      52        writeName( file, getFirstName() );
      53        writeName( file, getLastName() );
      54        file.writeDouble( getBalance() );
      55     } // end method write
      56
      57     // write a name to file; maximum of 15 characters
      58     private void writeName( RandomAccessFile file, String name )
      59        throws IOException                                       
      60     {
      61        StringBuffer buffer = null;
      62
      63        if ( name != null )
      64           buffer = new StringBuffer( name );
      65        else
      66           buffer = new StringBuffer( 15 );
      67
      68        buffer.setLength( 15 );
      69        file.writeChars( buffer.toString() );
      70     } // end method writeName
      71  } // end class RandomAccessAccountRecord
      Code:
       8  public class CreateRandomFile
       9  {
      10     private static final int NUMBER_RECORDS = 100;
      11
      12     // enable user to select file to open
      13     public void createFile()
      14     {
      15        RandomAccessFile file = null;
      16
      17        try // open file for reading and writing
      18        {
      19           file = new RandomAccessFile( "clients.dat", "rw" );
      20
      21           RandomAccessAccountRecord blankRecord =
      22              new RandomAccessAccountRecord();
      23
      24           // write 100 blank records
      25           for ( int count = 0 ; count < NUMBER_RECORDS; count++ )
      26              blankRecord.write( file );
      27
      28           // display message that file was created
      29           System.out.println( "Created file clients.dat." );
      30
      31           System.exit( 0 ); // terminate program
      32        } // end try
      33        catch ( IOException ioException )
      34        {
      35           System.err.println( "Error processing file." );
      36           System.exit( 1 );
      37        } // end catch
      38        finally
      39        {
      40           try
      41           {
      42              if ( file != null )
      43                 file.close(); // close file
      44           } // end try
      45           catch ( IOException ioException )
      46           {
      47              System.err.println( "Error closing file." );
      48              System.exit( 1 );
      49           } // end catch
      50        } // end finally
      51     } // end method createFile
      52  } // end class CreateRandomFile

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by khajeddin
        well, i'v read some where that for creating a random access file we should spesify an exact size for every record which we want to keep.
        for example if we want to create a file which can take 100 record and every record will occure 32 byte (at maximom size),we should give this size(32 byte) to the program.it means after finishing the program we can't have a record with size of 42 byte.
        Nonono, those were the days when IBM's VM (*), CMS, dinosaurs, mammoths
        and Marylin Monroe ruled the word. Think of a random access file as a semi-infinite
        sequence of bytes and a little finger; the little finger points to the current file
        position and you can read or write anything you want starting at the position to
        which that little fingers points.

        Originally posted by khajeddin
        now my question is : is it any way that we can make random access file which make it posible to write records with different size of byte,without risrict the program foe a maximom size?
        Yep, see above; nothing on earth prohibits you to do that.

        kind regards,

        Jos

        (*) nice thing, VM; a bit of an anachronism in those days though.

        Comment

        • khajeddin
          New Member
          • Nov 2006
          • 51

          #5
          well how can i do that ,in the above programs if i don't write the line:
          Code:
          public static final SIZE 72;
          does the program run correctly????
          if i create a random access file without ristriction,whi le using the program and updating a record( for example add some information to the record)doesn't it distibute the date of th next record?
          and in this condition how can i access to a record (because now i don't know howmany bytesi should move forward! )?

          Comment

          Working...