あなたのフォーマットが次のようなものであると仮定します
{string possibly with spaces} {integer}\r?\n
改行を検索し、最初のスペースが見つかるまで逆方向に作業する必要があります。数値を自分でデコードして に変換しint
たり、文字列に変換して解析したりできます。必要がない限り、整数は使用しません。これで、行の開始位置と整数の開始位置がわかったので、文字列をバイトとして抽出し、目的のエンコーディングを使用して文字列に変換できます。
これは、エンコーディングで改行とスペースが 1 バイトであることを前提としています。それらがマルチバイトバイトである場合、それはより複雑になりますが、それでも実行できます。
編集:次の例は印刷されます...
text: ' someString', number: 8
text: 'some other string', number: -88
コード
ByteBuffer bb = ByteBuffer.wrap(" someString 8\r\nsome other string -88\n".getBytes());
while(bb.remaining()>0) {
int start = bb.position(),end, ptr;
for(end = start;end < bb.limit();end++) {
byte b = bb.get(end);
if (b == '\r' || b == '\n')
break;
}
// read the number backwards
long value = 0;
long tens = 1;
for(ptr = end-1;ptr>= start;ptr--) {
byte b = bb.get(ptr);
if (b >= '0' && b <= '9') {
value += tens * (b - '0');
tens *= 10;
} else if (b == '-') {
value = -value;
ptr--;
break;
} else {
break;
}
}
// assume separator is a space....
byte[] bytes = new byte[ptr-start];
bb.get(bytes);
String text = new String(bytes, "UTF-8");
System.out.println("text: '"+text+"', number: "+value);
// find the end of the line.
if (bb.get(end) == '\r') end++;
bb.position(end+1);
}