I readed a file using Java and use HexDump to output the data. It looks like this: The first and second line: one:31 30 30 31 30 30 30 31 31 30 30 31 30 31 31 31 two: 30 31 31 30 30 31 31 30 31 31 30 30 31 31 30 31 I want to print the data between first "31 30 30 31"and the second "31 30 30 31".My ideal ouput is 31 30 30 31 30 30 30 31 31 30 30 31 30 31 31 31 30 31. But the real output is wrong,I think my code can not find the 31 30 30 31 in the data1.How to figure it out?
I Use jdk 1.7 and the software is idea
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.File;
public class TestDemo{
public static void main(String[] args) {
try {
File file = new File("/0testData/1.bin");
DataInputStream isr = new DataInputStream(newFileInputStream(file));
int bytesPerLine = 16;
int byteCount = 0;
int data;
while ((data = isr.read()) != -1) {
if (byteCount == 0)
System.out.println();
else if (byteCount % bytesPerLine == 0)
System.out.printf("\n",byteCount );
else
System.out.print(" ");
String data1 = String.format("%02X",data & 0xFF);
System.out.printf(data1);
byteCount += 1;
if(data1.contains("31 30 30 31")) {
int i=data1.indexOf("31 30 30 31",12);
System.out.println("find it!");
String strEFG=data1.substring(i,i+53);
System.out.println("str="+strEFG);
}else {
System.out.println("cannot find it");
}
}
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
}
My ideal ouput is 31 30 30 31 30 30 30 31 31 30 30 31 30 31 31 31 30 31. But the real output is:
31cannot find it 30cannot find it 30cannot find it 31cannot find it 30cannot find it 30cannot find it 30cannot find it 31cannot find it 31cannot find it 30cannot find it 30cannot find it 31cannot find it 30cannot find it 31cannot find it 31cannot find it 31cannot find it
30cannot find it 31cannot find it 31cannot find it 30cannot find it 30cannot find it 31cannot find it 31cannot find it 30cannot find it 31cannot find it 31cannot find it 30cannot find it 30cannot find it 31cannot find it 31cannot find it 30cannot find it 31cannot find it
31cannot find it 31cannot find it 31cannot find it 31cannot find it 30cannot find it 30cannot find it 30cannot find it 30cannot find it 30cannot find it 31cannot find it 30cannot find it 31cannot find it 30cannot find it 31cannot find it 31cannot find it 31cannot find it
31cannot find it 31cannot find it 30cannot find it 31cannot find it 31cannot find it 31cannot find it 31cannot find it 31cannot find it 31cannot find it 31cannot find it 30cannot find it 30cannot find it 31cannot find it 30cannot find it 31cannot find it 31cannot find it
30cannot find it 31cannot find it 31cannot find it 30cannot find it 30cannot find it 31cannot find it 31cannot find it 30cannot find it 30cannot find it 31cannot find it 30cannot find it 30cannot find it