473,395 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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?
Jun 16 '07 #1
4 3381
JosAH
11,448 Expert 8TB
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
Jun 16 '07 #2
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:
Expand|Select|Wrap|Line Numbers
  1.  4
  2.  5  import java.io.RandomAccessFile;
  3.  6  import java.io.IOException;     
  4.  7
  5.  8  public class RandomAccessAccountRecord extends AccountRecord
  6.  9  {
  7. 10      public static final int SIZE = 72;//here a size of records is spesified
  8. 11
  9. 12     // no-argument constructor calls other constructor with default values
  10. 13     public RandomAccessAccountRecord()
  11. 14     {
  12. 15        this ( 0, "", "", 0.0 );
  13. 16     } // end no-argument RandomAccessAccountRecord constructor
  14. 17
  15. 18     // initialize a RandomAccessAccountRecord
  16. 19     public RandomAccessAccountRecord( int account, String firstName,
  17. 20        String lastName, double balance )
  18. 21     {
  19. 22        super( account, firstName, lastName, balance );
  20. 23     } // end four-argument RandomAccessAccountRecord constructor
  21. 24
  22. 25     // read a record from specified RandomAccessFile
  23. 26     public void read( RandomAccessFile file ) throws IOException
  24. 27     {
  25. 28        setAccount( file.readInt() );
  26. 29        setFirstName( readName( file ) );
  27. 30        setLastName( readName( file ) );
  28. 31        setBalance( file.readDouble() );
  29. 32     } // end method read
  30. 33
  31. 34     // ensure that name is proper length
  32. 35     private String readName( RandomAccessFile file ) throws IOException
  33. 36     {
  34. 37        char name[] = new char[ 15 ], temp;
  35. 38
  36. 39        for ( int count = 0; count < name.length; count++ )
  37. 40        {
  38. 41           temp = file.readChar();
  39. 42           name[ count ] = temp;
  40. 43        } // end for
  41. 44
  42. 45        return new String( name ).replace( '\0', ' ' );
  43. 46     } // end method readName
  44. 47
  45. 48     // write a record to specified RandomAccessFile
  46. 49     public void write( RandomAccessFile file ) throws IOException
  47. 50     {
  48. 51        file.writeInt( getAccount() );
  49. 52        writeName( file, getFirstName() );
  50. 53        writeName( file, getLastName() );
  51. 54        file.writeDouble( getBalance() );
  52. 55     } // end method write
  53. 56
  54. 57     // write a name to file; maximum of 15 characters
  55. 58     private void writeName( RandomAccessFile file, String name )
  56. 59        throws IOException                                       
  57. 60     {
  58. 61        StringBuffer buffer = null;
  59. 62
  60. 63        if ( name != null )
  61. 64           buffer = new StringBuffer( name );
  62. 65        else
  63. 66           buffer = new StringBuffer( 15 );
  64. 67
  65. 68        buffer.setLength( 15 );
  66. 69        file.writeChars( buffer.toString() );
  67. 70     } // end method writeName
  68. 71  } // end class RandomAccessAccountRecord
  69.  
Expand|Select|Wrap|Line Numbers
  1.  
  2.  8  public class CreateRandomFile
  3.  9  {
  4. 10     private static final int NUMBER_RECORDS = 100;
  5. 11
  6. 12     // enable user to select file to open
  7. 13     public void createFile()
  8. 14     {
  9. 15        RandomAccessFile file = null;
  10. 16
  11. 17        try // open file for reading and writing
  12. 18        {
  13. 19           file = new RandomAccessFile( "clients.dat", "rw" );
  14. 20
  15. 21           RandomAccessAccountRecord blankRecord =
  16. 22              new RandomAccessAccountRecord();
  17. 23
  18. 24           // write 100 blank records
  19. 25           for ( int count = 0 ; count < NUMBER_RECORDS; count++ )
  20. 26              blankRecord.write( file );
  21. 27
  22. 28           // display message that file was created
  23. 29           System.out.println( "Created file clients.dat." );
  24. 30
  25. 31           System.exit( 0 ); // terminate program
  26. 32        } // end try
  27. 33        catch ( IOException ioException )
  28. 34        {
  29. 35           System.err.println( "Error processing file." );
  30. 36           System.exit( 1 );
  31. 37        } // end catch
  32. 38        finally
  33. 39        {
  34. 40           try
  35. 41           {
  36. 42              if ( file != null )
  37. 43                 file.close(); // close file
  38. 44           } // end try
  39. 45           catch ( IOException ioException )
  40. 46           {
  41. 47              System.err.println( "Error closing file." );
  42. 48              System.exit( 1 );
  43. 49           } // end catch
  44. 50        } // end finally
  45. 51     } // end method createFile
  46. 52  } // end class CreateRandomFile
  47.  
  48.  
Jun 17 '07 #3
JosAH
11,448 Expert 8TB
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.

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.
Jun 17 '07 #4
well how can i do that ,in the above programs if i don't write the line:
Expand|Select|Wrap|Line Numbers
  1. public static final SIZE 72;
does the program run correctly????
if i create a random access file without ristriction,while 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! )?
Jun 17 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Oin Zea | last post by:
Is it possible for to program to access a random file at the same time and perform actions like create a new record?
3
by: Rob B | last post by:
Hello, I am just starting to learn Python and was writing a simple script on my machine (Mac OS X 10.3.4), but I can't seem to import the random module: #!/usr/bin/env python import random
0
by: jeff | last post by:
i'm using "visual basic.net standard ver 2003". i'm running windows xp with sp2. i'm trying to make a random access file. it will make the file. but the file will have no data in it. my program is...
1
by: rayw | last post by:
I was wondering if there were some good on-line references to the various pros/cons with these types of file access at all? Ta rayw
3
by: VB.NET | last post by:
I'm using a mysql database and connecting my vb.net program to the DB over a network connection. i would like to bring this data over to a vb.net random access file. does anyone know how to...
3
by: Bruce | last post by:
I am building a WinForms app that uses Web Services access to a server for most of its data input/output, but I also need to persist some of its data to the local disk (basically as a cache of some...
6
by: comp.lang.php | last post by:
/** * Generate the random security image * * @access public * @param $willUseFilePath (default false) boolean to determine if you will be using a file path * @param mixed $filePath (optional)...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
39
by: Alan Isaac | last post by:
This may seem very strange, but it is true. If I delete a .pyc file, my program executes with a different state! In a single directory I have module1 and module2. module1 imports random and...
4
by: knuckels23 | last post by:
Hi All, I have a Random access file which is written using VB 6.0. I need to read this file using C#. The record used in VB to write the Random access file is as follows Type AA aa1 As...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.