>>2Not exactly the same. I am trying to check if there exists any data after a search term, which I am expecting to be in the file. There are two options: the search term might be at the end, or there might be something after it. If there is something after it, do something else. In this case, I made it so that it will save the contents of the stuff after the search term to a file.
I think I found something suitable:
String searchTerm = ""; //put what I want to find here, but I don't want to put it on /prog/
Scanner whatever = new Scanner(filename);
byte[3] tempBytes;
tempBytes[0] = whatever.nextByte();
tempBytes[1] = whatever.nextByte();
tempBytes[2] = whatever.nextByte();
boolean searchTermNotFound = true;
boolean stuffAfterSearchTerm = false;
do {
String tempByteString = tempBytes.toString();
if (tempByteString == searchTerm) {
searchTermNotFound = false;
if (whatever.hasNextByte()) {
stuffAfterSearchTerm = true;
}
} else if (whatever.hasNextByte()) {
tempBytes[0] = { whatever.nextByte() };
if (whatever.hasNextByte()) {
tempBytes[1] = { whatever.nextByte() };
} else {
System.out.println("not enough bytes to complete full string thing");
}
if (whatever.hasNextByte()) {
tempBytes[2] = { whatever.nextByte() };
} else {
System.out.println("not enough bytes to complete full string thing");
}
} else {
System.out.println("not enough bytes to complete full string thing");
}
} while (whatever.hasNextByte() && searchTermNotFound);
if (stuffAfterSearchTerm) {
System.out.println("this is where I would save the remaining contents to a file but I am lazy");
}
I guess this would work? I didn't even try it in an IDE yet, and there is probably a better way to do it. Is this even the right way to convert hex values to strings? Probably not. And I know my control structures are weird and inefficient. I'm tired so this is bad even for my standards.