Article View: borland.public.jbuilder.java.language
Article #8418Re: How can I read in file into a byte array?
From: Jim Gallagher
Date: Tue, 20 Mar 2001 22:58
Date: Tue, 20 Mar 2001 22:58
37 lines
1978 bytes
1978 bytes
Hi Ben,
> I need to read a file of indeterminate length into an array of bytes.
> What I am having trouble with:
>
> How can I know how big to create the array of bytes if I don't know
> how big the file is?
>
I had a similar problem and I solved it with the following ugly kludge.
Someone who really knows java will probably show us the right way to do
it, but in the meantime this may help:
byte[] b = new byte[1000000]; //1M= number of bytes in zip entry
byte[] work_b = new byte[1000000];
while (ix < 1000000) {
BytesRead = bis.read (work_b,0,b.length);
if (BytesRead == -1) {
break;
}
System.arraycopy(work_b,0,b,ix,BytesRead);
ix = ix + BytesRead;
}
The problem I had was that even if the file is greater than a million
bytes, it takes several iterations before the bis.read will return
b.length bytes. Java seems to pick a number out of a hat for BytesRead
- not sure why.
-Jim Gallagher
Zenia Software, Inc.
Technical Details
Message-ID:
<3AB7E088.1B05938B@earthlink.net>Path:
arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!nntp1.njy.teleglobe.net!teleglobe.net!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnews!208.176.139.98References:
<3AB7BC0C.60473976@borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: f74b3,2ea00c6aa2f0fa5a,start X-Google-Attributes: gidf74b3,public X-Google-ArrivalTime: 2001-03-23 03:50:11 PST Message-ID: <3AB7E088.1B05938B@earthlink.net> Date: Tue, 20 Mar 2001 17:58:16 -0500 From: Jim Gallagher <zenia@earthlink.net> X-Mailer: Mozilla 4.61 [en] (OS/2; U) X-Accept-Language: en MIME-Version: 1.0 Newsgroups: borland.public.jbuilder.java.language Subject: Re: How can I read in file into a byte array? References: <3AB7BC0C.60473976@borland.com> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit NNTP-Posting-Host: 208.176.139.98 X-Trace: 20 Mar 2001 17:46:57 -0800, 208.176.139.98 Lines: 37 Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!logbridge.uoregon.edu!news.maxwell.syr.edu!nntp1.njy.teleglobe.net!teleglobe.net!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnews!208.176.139.98