🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.language
Article #8440

Re: How can I read in file into a byte array?

#8440
From: Herbert Carl Meyer
Date: Tue, 20 Mar 2001 21:52
46 lines
2505 bytes
Start from:

public int InputStream.read( byte[] b) throws IOException

which will read some number of bytes into b. If no data is available, it
will return -1. If any data is available, it will read up to the length of
b, returning the number of bytes read. If any thing else is wrong, it will
throw an IOException.

So pick a number for the size of b, read a chunk at a time, and copy bytes
from b to the end of something else, until the read returns fewer bytes
than the length of b (including -1).

There is another call of the form:

read(    byte[]       b,
            int            offset,
            int            len);

that you  could use, if you had a good idea what the maximum size of the
file was.

Read the JDK javadoc for java.io for details. And don't panic.

"Ben Matterson (Borland)" wrote:

> Hello,
>
> Maybe this is a dead easy question and I am just dumb.
>
> 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?
>
> How can I read the whole file into the byte array?
> fileInputStream.read() reads one byte at a time, so it s seems like a
> while loop would do it. But if I am use "FileInputStream.read() != -1"
> as the test condition, where do the bytes that are read go? I don't
> get how I can test for end of file (-1) *and* put the returned bytes
> into the array.
>
> What am I missing? Can someone give me steer in the right direction?
>
> Ben
Technical Details
Message-ID: <3AB7D121.4496BC68@mediaone.net>
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!news-out.visi.com!hermes.visi.com!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnews!64.105.70.175
References: <3AB7BC0C.60473976@borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: f74b3,2ea00c6aa2f0fa5a
X-Google-Attributes: gidf74b3,public
X-Google-ArrivalTime: 2001-03-23 04:00:14 PST
Message-ID: <3AB7D121.4496BC68@mediaone.net>
Date: Tue, 20 Mar 2001 15:52:33 -0600
From: Herbert Carl Meyer <hcMeyer@mediaone.net>
Reply-To: hcMeyer@mediaone.net
Organization: Dickson/Unigage
X-Mailer: Mozilla 4.76 [en]C-CCK-MCD NSCPCD47  (WinNT; 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: 64.105.70.175
X-Trace: 20 Mar 2001 13:56:36 -0800, 64.105.70.175
Lines: 47
Path: arc1-ca.pugleaf.net!supernews.google.com!sn-xit-03!supernews.com!news-out.visi.com!hermes.visi.com!hub.org!hub.org!PeerFeed.KWeb.on.ca!dnews!64.105.70.175