What should you do?

You have recently completed the creation of a new application.
Domain.com requires you to ensure that this new application creates a file that contains an array of bytes.
What should you do?

You have recently completed the creation of a new application.
Domain.com requires you to ensure that this new application creates a file that contains an array of bytes.
What should you do?

A.
Use the following code:
public void WriteBytes (byte [] bytes){
FileStream fs = new FileStream (“C:file.txt”, FileMode.Create);
for (int i = 0; i < bytes.Length – 1; i++)
fs.Write(bytes [i]);
fs.Close ();
}

B.
Use the following code:
public void WriteBytes (byte [] bytes){
FileStream fs = new FileStream (“C:file.txt”, FileMode.Create);
for (int i = 0; i < bytes.Length – 1; i++)
fs.WriteByte(bytes [i]);
fs.Close ();
}

C.
Use the following code:
public void WriteBytes (byte [] bytes){
FileStream fs = new FileStream (“C:file.txt”, FileMode.Create);
fs.WriteByte(bytes, 0, bytes.Length);
fs.Close ();
}

D.
Use the following code:
public void WriteBytes (byte [] bytes){
FileStream fs = new FileStream (“C:file.txt”, FileMode.Create);
fs.Write(bytes, 0, bytes.Length);
fs.Close ();
}

Explanation:
Answer D
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.Create value indicates a new file will be created or, if one already exists, that it will be overwritten.
The FileStream class includes a Write method for writing an array of bytes.
The Write method takes a byte array, offsetvalue and total number of bytes as arguments.
The other method, WriteByte, takes a single argument of the type of byte, and 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.
Incorrect Answers:
A, C: You should not use the code fragments that invokes the Write method or the WriteBytes method
with only one argument because no such signatures exists in the FileStream class.
B: This code is unnecessary because the FileStream class also contains a Write method that takes a byte array as an argument.



Leave a Reply 1

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