• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Performing read/write to the file : how

 
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I wanted to know what is the recommended way to read/write text from the file.

I see the instructions say numeric data is read/written as DataInputStream while text data is read/stored contain only 8 bit character.

Should I be using RandomAccessFile or something else.

What is the recommended way of performing this.

Regards
Aruneesh
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, I believe we should be using the RandomAccessFile for it. Take a look at the tutorial available from Sun.

Cheers!
 
Aruneesh Salhotra
Ranch Hand
Posts: 167
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure how the files should be stored as.
After the header, should the data be stored as plain text.

Java uses UTF-16 as default encoding, and on opening the file when generated using UTF-16, it is comes like boxes/chars.

I am trying to use US-ASCII to write to the file, but the generate file doesnt seem to contain plain text.
I am using RandomAccessFile.

Here is like a sample code, I am writing.

Please help

raf.seek(length);

raf.writeChars(new String(lr.location.getBytes("US-ASCII"),"US-ASCII"));
raf.writeInt(lr.size);
raf.writeChars(new String(lr.rate.getBytes(),"US-ASCII"));
raf.writeChars(new String(lr.smoking.getBytes(),"US-ASCII"));
raf.writeChars(new String(lr.date.getBytes(),"US-ASCII"));
raf.writeChars(new String(lr.owner.getBytes(),"US-ASCII"));
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic