aspose file tools
The moose likes Beginning Java and the fly likes Find string in binary file 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 » Beginning Java
Reply Bookmark "Find string in binary file" Watch "Find string in binary file" New topic
Author

Find string in binary file

Ravi Shankarappa
Ranch Hand

Joined: Jan 09, 2010
Posts: 55
I have a binary file which contains a string followed by compressed data. I wrote a java code to read the file into a byte array. The size of the byte array can vary depending on the size of the data and the position of the string can also vary. What is the best way to accomplish this?
Once I know the position of the string, then I can make a new Inputstream of rest of the byte array and read the data, but finding the position of the string has stumped me.

Any help is much appreciated.

-Ravi
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

The best way to accomplish that is to design the contents of the file so that you know the things which you now don't know. One common technique is to prefix the binary portion by, say, a four-byte integer value containing the number of bytes in the binary portion.

But perhaps that has already been done? You could ask the person who gave you the file.
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3049
    
    1

You say the String is located before the compressed data. Isn't the string at the start of the file then? You have to be more clear about the format of the file.
Ravi Shankarappa
Ranch Hand

Joined: Jan 09, 2010
Posts: 55
Paul Clapham wrote:The best way to accomplish that is to design the contents of the file so that you know the things which you now don't know. One common technique is to prefix the binary portion by, say, a four-byte integer value containing the number of bytes in the binary portion.

But perhaps that has already been done? You could ask the person who gave you the file.


This is file generated by a vendor's program. Immediately following the "identifier string" are two integer values that define the total number of bytes that follow. Other than that not much is fixed.
Ravi Shankarappa
Ranch Hand

Joined: Jan 09, 2010
Posts: 55
Stephan van Hulst wrote:You say the String is located before the compressed data. Isn't the string at the start of the file then? You have to be more clear about the format of the file.


Correct, however, the string is not at the start of the file. There are a number of other parameters that precede the "identifier string" and the length of those is not fixed. Hence, the need to find the start of the string.

Since the size of the file is unknown at start of the program, I use the following code to read all the bytes as follows:


So now I have all the bytes in byte array fileContents. What is the position of the string lets say "Here34503450"? The size of the image is 3450 * 3450, and both are represented by 4 byte integer. Depending on the machine the file was created, it could be little-endian or big-endian, and this information is contained at the beginning of the file, so reading those two numbers is easy. However, where does "H" in "Here" is located?

-Ravi

*Note: Edited to correct a mistake
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Find string in binary file
 
Similar Threads
Opinions On Reading Data File
String and boolean memory
How to read binary file?
Reading data from files
problem converting to binary