Hello all,
I am relatively new to using Java's Output and Input streams. I know the basics and I know how to use them but what is driving me crazy is I am not entirely sure how it works. For example I know the following code writes a file from an InputStream to an OutputStream:
InputStream in = new FileInputStream(inputFile);
OutputStream out = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int len;
while((len = in.read(buffer))>0) {
out.write(buffer, 0, len);
}
So if some one could be so kind as to explain a little about the following I would really appreciate it. (I don't know much about bytes)
byte[] buffer = new byte[1024];
Now I know this is an array of bytes but what role does this line of code play in writing to an OutputStream and why a size of 1024 and not 4000 or some other number?
out.write(buffer, 0, len);
I know under the java docs it says that len is the amount of bytes to write but isn't it just writing one byte at a time anyway? Lets say len had a value of one all the time, would that simply just write one byte at a time to the Output stream?
out.write(byte[] b, int off, int len);
Now under the java docs it explains that the byte array b is "the data" which I don't understand, is the OutputStream writing 1024 bytes to the byte array and then writing that byte array to the Output file? Why do I need a byte array in the first place?
I know i'm asking a lot of questions but believe me I've tried to figure it out on my own. I've spent the last 4 hours trying to figure out how this code works by reading the java docs and looking it up on google etc but I can't seem to find a good tutorial or explanation for the questions I've asked. So thank you for reading this and thank you in advance for any input, its most appreciated.