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?
4 3381
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
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: -
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
-
-
-
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
-
-
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.
well how can i do that ,in the above programs if i don't write the line: - 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! )?
Sign in to post your reply or Sign up for a free account.
Similar topics
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?
|
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
|
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...
|
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
|
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...
|
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...
|
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)...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |