• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

reading binary file in java

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
i have to read a binary file whoes format is

struct emp
{
char name[20];
int age;
char add[50];
}

this struct is used to write this file i have to read this file in java application and extract these field ,what i did is read this file in a byte array like byte buf[74] means its length is same as the record length of file,i read a record in byte array and try to extract data from array but output of the program is not expected when i try to print a name file then first time it print the correct name but latter time it print the wrong name

for ex in record name are as follows
1->vyom
2->neelmani
3->neha
4->ram

it first print vyom then neelmani then nehamani then ramamani,it means if the latter field value is smaller then previous then it append the rest field also

please help me with code if possible how to parse a structured binary file in java
 
Bartender
Posts: 1840
Eclipse IDE Ruby Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you please post your code so that we can see what it is doing? Offhand, I'd say that you are using the same buffer arrya for each record but not erasing the contents between readings.
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

How are you finding the end of the name and add character sequences? Is there supposed to be a 0 byte, or what?
How are you converting the byte[] to a String?
Bill
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the use of struct in your initial post, I assume the file is written by a program written in C or C++. One problem you will encounter is that the struct definition still doesn't tell us what the format of the file is. Do you know exactly how the fields for this struct were written to the file? Can you get a hold of the code that does this? If you are allowed to post it here, that will probably be helpful.

With that said, you will probably need to look at the various stream classes in the java.io package. If you are unfamiliar with Java I/O, you should find a tutorial on it before you go much further.

HTH

Layne
 
Men call me Jim. Women look past me to this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!