Chapter 9 – Working with Files, Streams, and Serialization
- What is the difference between using the
Fileclass and theFileInfoclass?
Answer: The File class has static methods, so it cannot be instantiated. It is best used for one-off tasks such as copying a file. The FileInfo class requires the instantiation of an object that represents a file. It is best used when you need to perform multiple operations on the same file.
- What is the difference between the
ReadBytemethod and theReadmethod of a stream?
Answer: The ReadByte method returns a single byte each time it is called and the Read method fills a temporary array with bytes up to a specified length. It is generally best to use Read to process blocks of bytes at once.
- When would you use the
StringReader,TextReader, andStreamReaderclasses?
Answer:StringReader is used for efficiently reading from a string stored in memory. TextReader is an abstract class that StringReader and StreamReader both inherit from, for their shared functionality...