Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading binary file byte by byte

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?

like image 612
alwynmalan Avatar asked Sep 20 '25 06:09

alwynmalan


2 Answers

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.

like image 96
Joop Eggen Avatar answered Sep 22 '25 18:09

Joop Eggen


it is simple example:

   public class CopyBytes {
    public static void main(String[] args) throws IOException {

        FileInputStream in = null;
        FileOutputStream out = null;

        try {
            in = new FileInputStream("xanadu.txt");
            out = new FileOutputStream("outagain.txt");
            int c;

            while ((c = in.read()) != -1) {
                out.write(c);
            }
        } finally {
            if (in != null) {
                in.close();
            }
            if (out != null) {
                out.close();
            }
        }
    }
}

If you want to read text(characters) - use Readers, if you want to read bytes - use Streams


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!