0

I'm new to Java and a I need to read a binary file and display its contents converted as integers. The file has this structure:

{client#, position 1, size 32 | 
category, position 33, size 10 | 
type, position 43, size 10 | 
creditlimit, position 53, size 20}

I need just a guide on what classes to use and a convertion example, a little snipet will be appreciated.

3
  • 1
    Show us the little snippet of code you have tried. Commented Nov 20, 2014 at 13:48
  • 2
    When you say binary file, do you mean a text file as above or true, binary data (e.g., bytes in some encoding that is not text)? Commented Nov 20, 2014 at 13:49
  • What code have you tried so far? Commented Nov 20, 2014 at 13:50

3 Answers 3

2

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.
}
Sign up to request clarification or add additional context in comments.

Comments

1

If i understand your question correct, you should use the NIO Package.
With the asIntBuffer() from the byteBuffer class, you can get an IntBuffer view of a ByteBuffer. And by calling get(int[] dst) you could convert it to integers.

The initial ByteBuffer is available by using file channels.

Comments

1

If you work with binary data, may be JBBP will be comfortable way for you, to parse and print the data structure with the framework is very easy (if I understood the task correctly and you work with byte fields), the example parsing whole input stream and then print parsed data to console

@Bin class Record {byte [] client; byte [] category;  byte [] type; byte [] creditlimit;};
@Bin class Records {Record [] records;};
Records parsed = JBBPParser.prepare("records [_] {byte [32] client; byte [10] category; byte [10] type; byte [20] creditlimit;}").parse(THE_INPUT_STREAM).mapTo(Records.class);
System.out.println(new JBBPTextWriter().Bin(parsed).toString());

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.