Reading binary file byte by byte

17,946

Since java 7 it is not needed to read byte by byte, there are two utility function in Files:

Path path = Paths.get("C:/temp/test.txt");

// Load as binary:
byte[] bytes = Files.readAllBytes(path);
String asText = new String(bytes, StandardCharset.ISO_8859_1);

// Load as text, with some Charset:
List<String> lines = Files.readAllLines(path, StandardCharsets.ISO_8859_1);

As you want to read binary data, one would use readAllBytes.

String and char is for text. As opposed to many other programming languages, this means Unicode, so all scripts of the world may be combined. char is 16 bit as opposed to the 8 bit byte.

For pure ASCII, the 7 bit subset of Unicode / UTF-8, byte and char values are identical.

Then you might have done the following (low-quality code):

int fileLength = (int) path.size();
char[] chars = new char[fileLength];
int i = 0;
int data;
while ((data = inputStream.read()) != -1) {
    chars[i] = (char) data; // data actually being a byte
    ++i;
}
inputStream.close();

String text = new String(chars);

System.out.println(Arrays.toString(chars));

The problem you had, probably concerned the unwieldy fixed size array in java, and that a char[] still is not a String.

For binary usage, as you seem to be reading serialized data, you might like to dump the file:

int i = 0;
int data;
while ((data = inputStream.read()) != -1) {
    char ch = 32 <= data && data < 127 ? (char) data : ' ';
    System.out.println("[%06d] %02x %c%n", i, data, ch);
    ++i;
}

Dumping file position, hex value and char value.

Share:
17,946
alwynmalan
Author by

alwynmalan

Updated on June 04, 2022

Comments

  • alwynmalan
    alwynmalan almost 2 years

    I've been doing research on a java problem I have with no success. I've read a whole bunch of similar questions here on StackOverflow but the solutions just doesn't seem to work as expected.

    I'm trying to read a binary file byte by byte.

    I've used:

    while ((data = inputStream.read()) != -1) 
    

    loops...

    for (int i = 0; i < bFile.length; i++) {
    

    loops...

    But I only get empty or blank output. The actual content of the file I'm trying to read is as follows:

    ¬í sr assignment6.PetI¿Z8kyQŸ I ageD weightL namet Ljava/lang/String;xp > @4 t andysq ~ @bÀ t simbasq ~ @I t wolletjiesq ~
    @$ t rakker

    I'm merely trying to read it byte for byte and feed it to a character array with the following line:

    char[] charArray = Character.toChars(byteValue);
    

    Bytevalue here represents an int of the byte it's reading.

    What is going wrong where?

  • Admin
    Admin over 9 years
    You mean data position, hex value and char value, right?
  • Joop Eggen
    Joop Eggen over 9 years
    Yes %d = number, %x = hexadecimal number, %c char.