A question about Array plz !!!

J

Joseph

hi, I just began with studying java and have a question about reading
bytes from file(fileA).Every time I make it read into a byte array with
size 256.I used a while loop to see whether the next byte is a -1,if it
is then stop .

int c,j=0;
mByteData=new byte[256];
while ((c=mByteSource.read())!= -1&&j<256){
mByteData[j]=(byte)c;
j++;
}


Q1: what is the second array like if the file size is 270kb ?(first 14
are bytes from file and rest are null ? or empty ? or zero ?)

Q2 : if I copy the array byte after byte using a for loop like


byte[] b2=new byte[256];
for(int i=0;i<256;i++){
b2=b1;

}

how can I write bytes to fileB and make fileA and fileB identical?
(MD5 sum)
because every time I found from 14th byte all the rest bytes
are all zeros ,there is no -1 and I do not know where to stop if I
want to write them to fileB.

Am I clear enough ?
 
C

chris

Joseph said:
hi, I just began with studying java and have a question about reading
bytes from file(fileA).Every time I make it read into a byte array with
size 256.I used a while loop to see whether the next byte is a -1,if it
is then stop .

int c,j=0;
mByteData=new byte[256];
while ((c=mByteSource.read())!= -1&&j<256){
mByteData[j]=(byte)c;
j++;
}

This is horrid code. Do yourself a favour and use the read() method that
takes a byte array as parameter. It'll tell you how many bytes it read.
Q1: what is the second array like if the file size is 270kb ?(first 14
are bytes from file and rest are null ? or empty ? or zero ?)

(ITYM 270 bytes)

Each element of mByteData must hold a value between -128 and +127; there is
no such thing as "null" or "empty". As it happens, in Java all numeric
arrays (byte/short/int/long/float/double) are initialised to contain all
zeros. (Boolean array are initialised to false, arrays of references are
initialised to null).
Q2 : if I copy the array byte after byte using a for loop like


byte[] b2=new byte[256];
for(int i=0;i<256;i++){
b2=b1;

}

how can I write bytes to fileB and make fileA and fileB identical?
(MD5 sum)
because every time I found from 14th byte all the rest bytes
are all zeros ,there is no -1 and I do not know where to stop if I
want to write them to fileB.


You need to remember how many bytes were in fileA, and only write that
number of bytes to fileB. For example if you persist in using this horrid
code then you need to change "i<256" to "i<j" in your second loop.

The reason you do not find -1 in your byte array is that you wrote the
while loop this way:
while ((c=mByteSource.read())!= -1&&j<256){
So the first time you read -1 you will break out of the loop, without
executing
mByteData[j]=(byte)c;

I really hate this damn machine
I wish that they would sell it
It never does quite what I want
But only what I tell it ...
Am I clear enough ?
You are clear as a bell, but you have a lot to learn about programming (in
any language, not just Java). :)

Good luck

Chris
 
R

Roedy Green

while ((c=mByteSource.read())!= -1&&j<256){
mByteData[j]=(byte)c;

The -1 is not data, just a marker. Note you jump out of the loop as
soon as you hit it and don't store it in your array.

The only indication you have of how much stuff you read is the value
of j when you leave the loop. You know j points to the next empty
slot (or one past the end).

You could now create an array j-1 bytes long and copy into that if you
want one exactly the right size. See System.arraycopy.

See http://mindprod.com/jgloss/gotchas.html#ARRAY for some basics.
You need a Java text book for this sort of stuff.

Even an out of date one will do.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,269
Latest member
vinaykumar_nevatia23

Latest Threads

Top