reading data into a byte array

How would I go about reading for example a zip file, or an image into a byte array. Thanks
[104 byte] By [jayclarkea] at [2007-10-2 1:17:33]
# 1

Simple (but not the best):

Create a byte array, loop, put the result on in.read() into the array? If you need to grow the array, use System.copyArray.

A better option might be too look at

[url=http://java.sun.com/j2se/1.5.0/docs/api/java/io/ByteArrayInputStream.html]ByteArrayInputStream[/url]

and

[url=http://java.sun.com/j2se/1.5.0/docs/api/java/io/InputStream.html#read(byte[])]InputStream.read(byte[][/url].

mlka at 2007-7-15 18:38:47 > top of Java-index,Java Essentials,Java Programming...
# 2

import java.io.*;

public class ReadFile {

public static byte[] read(File file) throws IOException {

long length = file.length();

if (length > Integer.MAX_VALUE)

throw new IOException("too big");

DataInputStream in = null;

try {

in = new DataInputStream(new FileInputStream(file));

byte[] data = new byte[(int)length];

in.readFully(data);

return data;

} finally {

if (in!=null)

in.close();

}

}

}

jTooheya at 2007-7-15 18:38:47 > top of Java-index,Java Essentials,Java Programming...