Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

buffered reading for RandomAccessFile  RSS feed

 
Alice Peabody
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • 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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • 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.
 
kalyani anne
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!