I assume that position 1 actually is 0; the first byte.
Also it seems the file format is of fixed size records, probably with ASCII in the bytes.
To check the data, I start with taking the fields in Strings. Converting them to long/int could loose information on the actual content.
The following uses a sequential binary file. Faster would be a memory mapped file, but this is acceptable and short.
Hold the client data:
class Client {
String clientno;
String category;
String type;
String position;
String creditlimit;
@Override
public String toString() {
return String.format("Client# %s, categ %s, type %s, pos %s, creditlimit %s%n",
clientno, category, type, position, creditlimit);
}
}
Read the file:
// Field sizes:
final int CLIENT_NO = 32;
final int CATEGORY = 10;
final int TYPE = 10;
final int CREDIT_LIMIT = 20;
final int RECORD_SIZE = CLIENT_NO + CATEGORY + TYPE + CREDIT_LIMIT;
byte[] record = new byte[RECORD_SIZE];
try (BufferedInputStream in = new BufferedInputStream(
new FileInputStream(file))) {
for (;;) {
int nread = in.read(record);
if (nread < RECORD_SIZE) {
break;
}
Client client = new Client();
int offset = 0;
int offset2 = offset + CLIENT_NO;
client.clientno = recordField(record, offset, offset2 - offset);
offset = offset2;
int offset2 = offset + CATEGORY;
client.category = recordField(record, offset, offset2 - offset);
offset = offset2;
int offset2 = offset + TYPE;
client.type = recordField(record, offset, offset2 - offset);
offset = offset2;
int offset2 = offset + CREDITLIMIT;
client.creditlimit = recordField(record, offset, offset2 - offset);
System.out.println(client);
}
} // Closes in.
with a field extraction:
private static String recordField(byte[] record, int offset, int length) {
String field = new String(record, offset, length, StandardCharsets.ISO_8859_1);
// Use ASCII NUL as string terminator:
int pos = field.indexOf('\u0000');
if (pos != -1) {
field = field.substring(0, pos);
}
return field.trim(); // Trim also spaces for fixed fields.
}