Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Reading byte arrays from a file.

807589Oct 27 2008 — edited Oct 27 2008
I can't seem to find a way to read data from a file into a byte array. I saved the file using:
DataOutputStream dos = new DataOutputStream(new FileOutputStream(filechooser.getSelectedFile()));
for(int i = 0; i < bytearray.length; i++)
  dos.writeByte((int) bytearray);
I need to be able to read the data back into an array, but the DataInputStream.readFully(byte[]); just throws a NullPointerException because I can't initialize the byte array to hold all the bytes in the file, because I don't know how many bytes are in the file (and I'm not sure how to find out...).                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Nov 24 2008
Added on Oct 27 2008
5 comments
596 views