aspose file tools
The moose likes Java in General and the fly likes Binary File Parsing Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Binary File Parsing" Watch "Binary File Parsing" New topic
Author

Binary File Parsing

James Basller
Ranch Hand

Joined: Sep 07, 2008
Posts: 58
Hi All,

I have one binary file (.dat). I have to parse that file. So, can anyone suggest me how I can achieve this?

Is there any 3rd party API available like for XML I am using VTD-XML parsing ?

Thanks!!!
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 35237
    
    7
I'm not sure what the connection to XML might be, but if you don't have a library that can handle whatever format the file has, you'll need to write a parser based on the java.io classes (like FileInputStream).


Android appsImageJ pluginsJava web charts
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12267
    
    1
What information do you have on the structure of the data? What generates your .dat file?

Bill

Java Resources at www.wbrogden.com
James Basller
Ranch Hand

Joined: Sep 07, 2008
Posts: 58
William Brogden wrote:What information do you have on the structure of the data? What generates your .dat file?

Bill


binary files are coming from the hardware device. I have a file named xyz.dat which contains lines like below

00000000h: 76 04 00 01 00 FF 0F 11 FF 09 07 03 15 ; v....

this is a kind of lines in my file above is not complete line

can anyone suggest how can I parse this kind of files???

Thanks!!!
Jesper de Jong
Java Cowboy
Bartender

Joined: Aug 16, 2005
Posts: 12911
    
    3

As Ulf suggested, you can read the contents of a binary file using FileInputStream. What you should then do with the bytes that you read, entirely depends on what the format of the file is and what your program needs to do with it. Without knowing what the bytes in the file mean, you can't do anything useful with it and nobody can tell you. You should lookup documentation of the device to find out what the format of the file is.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12267
    
    1
When parsing a byte[] remember that any operation looking at a byte is going to automatically promote the value to a signed int so your FF byte will become -1.

Therefore, use a bitwise mask with 0xFF before checking the value.

Bill
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Binary File Parsing
 
Similar Threads
reading xml file dynamically
Parsing data file to XML?
Generic XML parsing
Facing Problem with String replaceAll method
Huge XML file.