FileStream question

Can someone tell me if the explanation of this code bellow is correct? I'm having a problem on what the while statement mean...

 using (FileStream fs = fi.Open(FileMode.Open, FileAccess.Read))
           //Create a byte array b of size 1024
            byte[] b = new byte[1024];
           //Declare an instance of the UTF8Encoding
            UTF8Encoding temp = new UTF8Encoding(true);

            // Not sure what this code does bellow?
            while (fs.Read(b,0,b.Length) > 0)

                //Try to write to the file.
            catch (Exception e)
                Console.WriteLine("Writing was disallowed, as expected: {0}",
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Shaun KlineLead Software EngineerCommented:
The while statement will read data from the file stream in 1024 bytes and write those bytes out to the console using UTF-8 encoding.
käµfm³d 👽Commented:
Small correction to Shaun_Kline's statement above:

The while statement will read data from the file stream in *at most* 1024 bytes...
The last iteration of the while could read less than 1024 bytes.

And actually you should be capturing the number of bytes read by the Read method and using that value as the length to the Write method. Since you are reusing the same buffer, there will be junk at the end of the buffer from the last read which will not get overwritten by the next Read. You only want to write out as much as you read, not the full length of the buffer.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.