What should you do?

You are currently in the process of creating an application that reads binary information from a file.
You need to ensure that the only the first kilobyte of data is retrieved.
What should you do?

You are currently in the process of creating an application that reads binary information from a file.
You need to ensure that the only the first kilobyte of data is retrieved.
What should you do?

A.
Use the following code:
FileStream fs = new FileStream(“C:file.txt”, FileMode.Open);
BufferedStream bs = new BufferedStream (fs);
byte [ ] bytes = new byte [1023];
bs.Read (bytes, 0, bytes.Length);
bs.Close ();
for (int i = 0; i < bytes.Length-1; i++)
Console.WriteLine (“{0} : {1}”, I, bytes [i]);

B.
Use the following code:
FileStream fs = new FileStream(“C:file.txt”, FileMode.Open);
byte [ ] bytes = new byte [1023];
fs.Read (bytes, 0, bytes.Length);
fs.Close ();
for (int i = 0; i < bytes.Length-1; i++)
Console.WriteLine (“{0} : {1}”, I, bytes [i]);

C.
Use the following code:
FileStream fs = new FileStream(“C:file.txt”, FileMode.Open);
BufferedStream bs = new BufferedStream (fs);
byte [ ] bytes = new byte [1023];
bytes = bs.ReadAllBytes (0, 1023);
bs.Close ();
for (int i = 0; i < bytes.Length-1; i++)
Console.WriteLine (“{0} : {1}”, I, bytes [i]);

D.
Use the following code:
FileStream fs = new FileStream(“C:file.txt”, FileMode.Open);
BufferedStream bs = new BufferedStream (fs);
byte [ ] bytes = new byte [1023];
bs.Read (bytes);
bs.Close ();
for (int i = 0; i < bytes.Length-1; i++)
Console.WriteLine (“{0} : {1}”, I, bytes [i]);

Explanation:
The FileStream constructor accepts a string argument as the file path and a FileMode enumeration value.
The FileMode enumeration value indicates the file stream will be used, and includes the values Append, Create, CreateNew, Open, and Truncate.
The FileMode.Open value indicates a file will be opened if existing, or else a FileNotFoundException object will be thrown.
An array of 1024 bytes is instantiated.
The Read method takes the byte array, offset value, and total number of bytes as arguments. The other method ReadByte returns a single byte at a time, but it requires manual iteration to write an array.
Like all streams, the FileStream object has a Close method, which should be called after work is done with the stream.
The Console.WriteLine method is invoked to display the byte index and byte value.
Incorrect Answers:
A, C, D: You should not use the code that specifies a buffered stream BECAUSE the FileStream class IS ALREADY a buffered stream. Also, you should not use the code fragments that invoke the ReadAllBytes method and
the Read method with the incorrect arguments because no such method signatures exist.



Leave a Reply 1

Your email address will not be published. Required fields are marked *


seenagape

seenagape

I choose B