0

I have a java program that stores a byte array of 128 bytes length in a MySQL "BINARY (128)" field.

Then, with PHP I access to the database and I give the option to export this data into a file, so I unpack() the binary data, and write it into a file.

This file then, has to be read in a Java program that I am writing, but I can't find how I have to read this data. Any suggestion?

I tried with:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
int i = 0;
while (i < 128) {
  System.out.println(baos.read());
  i++;
}

But bis.read() returns an int, and the byte array I sent to the mysql db was a string: "text".getBytes("utf-8");

Thank you,

1
  • 1
    A ByteArrayOutputStream is only for outputting/writing. It doesn't have a read() method. Commented Mar 29, 2011 at 10:08

3 Answers 3

1

To combine the answers of @Ricardo and @MarcoS

 DataInputStream dis = new DataInputStream(new FileInputStream("my-data-file.dat"));
 try {
     byte[] bytes = new byte[128];
     dis.readFully(bytes);
     // read some more.

 } finally {
     try {
        dis.close();
     } catch(IOException ignored) { }
 }
Sign up to request clarification or add additional context in comments.

Comments

1

Why don't you use a FileInputStream? (http://download.oracle.com/javase/1.4.2/docs/api/java/io/FileInputStream.html)

The read() method reads a byte, though it returns an int.

1 Comment

+1: Best to use references to Java 6 Javadocs as both Java 1.4 and 5.0 are EOL now.
1

Have you tried with Data Streams?

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.