• Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading blocks of data from files

 
Prabhjot Lamba
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi

i have a small problem. hope somebody helps me out.

i will be able to explain myself better if i present a c++ program logic to read a file with structures.

in c++ we would..

step 1. create a structure like
struct
{ int a;
int b
float c;
}obj1;

step 2. open a file and read the contents of the file into the structure
[ with the read command specifying obj1 ] and then access the elements with obj1.a , obj1.b and obj1.c.

Now my problem is to replicate the above logic in java. i am sure (???) we cannot create structures in java. how will we read a particular group of data from a file in one read operation. what i finally want to achieve is to read a bitmap file and for that i need a structure for reading the bmp file header and other info.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create a structure in Java by using a class definition.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Prabhjot]: how will we read a particular group of data from a file in one read operation.

You can't, really. There's not a direct equivalent to the C++ trick here. If you control both the writing and the reading of the file, and you're allowed to use any format you want, then you could use object serialization here. But if the file format is not under your control, then that's a bad idea. I would recommend using a DataInputStream here to read each field individualally in whatever order the file format puts them. And probably use a BufferedInputStream to improve efficiency:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!