Win a copy of Head First Android this week in the Android forum!
  • 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

buffered reading for RandomAccessFile

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am reading/writing/updating large data files and the RandomAccessFile() seems the best fit. The only problem is that using the primitive read member functions like readInt(), readFloat() etc takes too long.
It seems the read (byte[],int off, int len) function would help me with buffering my reads, but I'm still green under the collar when it comes to manipulating Java primitive types.
I'm used to c where I can use memcpy to copy from a buffer into an int, or a float. How do I read in a block of bytes and then save it as the appropriate types? What is the equiv in Java for:
char buffer[20];
int ival;
float fval;
char cval;
memcpy (&ival, buffer, 4);
memcpy (&fval, &buffer[4], 4);
memcpy (&cval, &buffer[8], 1);

Many thanks.
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

There are a variety of ways to encode a String - you may also want to user the read(byte[]) method and then use a new String[byte[], String) constructor to specify the encoding used in the byte array. It depends on exactly how the String data was originally written. But if it was written using writeUTF(String), you can read it with readUTF().
 
Jim Yingst
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I should add that in my experience, RandomAccessFile is usually pretty slow for most applications. It's a jack-of-all-trades class; it does a lot of different things, but isn't particularly good at anyof them. You can usually create a combination of streams that does what you want with reater efficiency.
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am working with file to read write and update the records,can u please send me the code..if ur problem is fixed.
thanku
u can send it to k_manne@yahoo.com
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic