🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #18071

Random Access File

#18071
From: Sam C. King
Date: Sat, 15 Aug 1998 00:00
80 lines
2873 bytes
RandomAccessFile problems
In the Book: Java How to Program by Deitel & Deitel p777
There is a perfectly good example of Creating, Reading,
and Writing a Random Access File.  Now, of course, it is
worthless because someone has seen fit to change the
language, i.e. Depreciation.
So, now what works? I am prompted to ask the question
since now some 22 new books on Java here on my desk are
obsolete!  Can you point me to some text that show, with
working examples of code, the new version of Java.  I
happened upon "The Java Class Libraries Second Edition,
Volume 1 and Volume 2, and thought I had all the answers.
But, no such luck.

On page 1407 of Volume 1 they show an example of a
Random Access File that is totally impractical.  First,
they writeChars(c), but never read them back.  Is there
no such thing as readChars(c) ?  Why not?  Then they
assume that everyone's name and every word on the planet
has the same number of characters in it.  How else would:
String c = "abc"; raf.writeBytes(c); be of any use to
anybody?  In every language I've ever written, every
element or field in a random access file must have an
unchangable capacity, ie field1 = 20 characters, field2
= 10 characters, field3 = 20 characters, field4 = double,
etc.  Now if you wanted to put just 3 characters like
"abc" into field1, that's fine.  But field1's capacity
cannot change within a random access file.

I have tried in vane, just about everything I can think
of to create the simple file outlined in deitel's book
on p777, with no luck.

From what I've read, I really thought that the following
was going to work:

String s1 = "abc";
byte[] b1 = new byte[15];
b1 = s1.getBytes();
raf.write(b1,0,15);

String s2 = "efghi"
byte[] b2 = new byte[15];
b2 = s2.getBytes();
raf.write(b2,0,15);


String s3 = "lmn"
byte[] b3 = new byte[15];
b3 = s3.getBytes();
raf.write(b3,0,15)

//// Now read the random access file: raf

byte[] b1 = new byte[15];
String s1 = new String(b1);
System.out.println(s1);

byte[] b2 = new byte[15];
String s2 = new String(b2);
System.out.println(s2);

byte[] b3 = new byte[15];
String s3 = new String(b3);
System.out.println(s3);


I expected to get:
abc
efghi
lmn

Instead, I got
abcefglmn

Nuts!  I am worn out working with this thing.

Does anyone have an answer
Thanks
Sam   samcking@olypen.com
Technical Details
Message-ID: <35D62B58.DF97BDCA@olypen.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,2ac3827aa401f7bc,start
X-Google-Attributes: gidf74b3,public
From: "Sam C. King" <samcking@olypen.com>
Subject: Random Access File
Date: 1998/08/15
Message-ID: <35D62B58.DF97BDCA@olypen.com>
X-Deja-AN: 381539271
Content-Transfer-Encoding: 7bit
Organization: Another Netscape Collabra Server User
Content-Type: text/plain; charset=us-ascii
Mime-Version: 1.0
Newsgroups: borland.public.jbuilder.java.language