There are many mechanisms for recording state information about applications, other than creating a file full of the information. One example of this type of mechanism is the Windows Event Log, where informational, security, and error states can be logged during an application’s progress. One of the primary reasons for creating a log file is to assist in troubleshooting or to debug your code in the field. If you are shipping code without some sort of debugging mechanism for your support staff (or possibly for you in a small company), we suggest you consider adding some logging support. Any developer who has spent a late night debugging a problem on a QA machine, or worse yet, at a customer site, can tell you the value of a log of the program’s actions.
If you are writing character information to a file, the simplest method is to use the Write and WriteLine methods of the StreamWriter class to write data to the file. These two methods are overloaded to handle any of the primitive values (except for the byte data type), as well as character arrays. All of this information is written to the file as character text, not as the underlying primitive type.
If you need to write byte data to a file, consider using the Write and WriteByte methods of the FileStream class. These methods are designed to write byte values to a file. The WriteByte method accepts a single byte value and writes it to the file, after which the pointer to the current position in the file is advanced to the next value after this byte. The Write method accepts an array of bytes that can be written to the file, after which the pointer to the current position in the file is advanced to the next value after this array of bytes. The Write method can also choose a range of bytes in the array in which to write to the file.
The Write method of the BinaryWriter class overloaded similarly to the Write method of the StreamWriter class. The main difference is that the BinaryWriter class’s Write method does not allow formatting. This allows the BinaryReader to read the information written by the BinaryWriter as its underlying type, not as a character or a byte.
Once we have the data written to the file, we can read it back out. The first concern when reading data from a file is not to go past the end of the file. The StreamReader class provides a Peek method that looks0‰9¢ã¡±but does not retrieve0‰9¢ã¡±the next character in the file. If the end of the file has been reached, a -1 is returned. Likewise, the Read method of this class also returns a -1 if it has reached the end of the file. The Peek and Read methods can be used in the following manner to make sure that you do not go past the end of the file:
StreamReader streamReader = new StreamReader("data.txt"); while (streamReader.Peek() != -1) { Console.WriteLine(streamReader.ReadLine()); } streamReader.Close();
or
StreamReader streamReader = new StreamReader("data.txt"); string text = ""; while ((text = streamReader.Read()) != -1) { Console.WriteLine(text); } streamReader.Close();
The main differences between the Read and Peek methods are that the Read method actually retrieves the next character and increments the pointer to the current position in the file by one character, and the Read method is overloaded to return an array of characters instead of just one. If the Read method is used that returns an array buffer of characters and the buffer is larger than the file, the extra elements in the buffer array are set to an empty string.
The StreamReader also contains a method to read an entire line up to and including the newline character. This method is called ReadLine. This method returns a null if it goes past the end of the file. The ReadLine method can be used in the following manner to make sure that you do not go past the end of the file:
StreamReader streamReader = new StreamReader("data.txt"); string text = ""; while ((text = streamReader.ReadLine()) != null) { Console.WriteLine(text); } streamReader.Close();
If you simply need to read the whole file in at one time, use the ReadToEnd method to read the entire file in to a string. If the current position in the file is moved to a point other than the beginning of the file, the ReadToEnd method returns a string of characters starting at that position in the file and ending at the end of the file.
The FileStream class contains two methods, Read and ReadByte, which read one or more bytes of the file. The Read method reads a byte value from the file and casts that byte to an int before returning the value. If you are explicitly expecting a byte value, consider casting the return type to a byte value:
FileStream fileStream = new FileStream("data.txt", FileMode.Open); byte retVal = (byte) fileStream.ReadByte();
However, if retVal is being used to determine whether the end of the file has been reached (i.e., retVal == -1 or retVal == 0xffffffff in hexadecimal), you will run into problems. When the return value of ReadByte is cast to a byte, a -1 is cast to 0xff, which is not equal to -1 but is equal to 255 (the byte data type is not signed). If you are going to cast this return type to a byte value, you cannot use this value to determine whether you are at the end of the file. You must instead rely on the Length Property. The following code block shows the use of the return value of the ReadByte method to determine when we are at the end of the file:
FileStream fileStream = new FileStream("data.txt", FileMode.Open); int retByte = 0; while ((retByte = fileStream.ReadByte()) != -1) { Console.WriteLine((byte)retByte); } fileStream.Close();
This code block shows the use of the Length property to determine when to stop reading the file:
FileStream fileStream = new FileStream("data.txt", FileMode.Open); long currPosition = 0; while (currPosition < fileStream.Length) { Console.WriteLine((byte) fileStream.ReadByte()); currPosition++; } fileStream.Close();
The BinaryReader class contains several methods for reading specific primitive types, including character arrays and byte arrays. These methods can be used to read specific data types from a file. All of these methods, except for the Read method, indicate that the end of the file has been reached by throwing the EndOfStreamException. The Read method will return a -1 if it is trying to read past the end of the file. This class contains a PeekChar method that is very similar to the Peek method in the StreamReader class. The PeekChar method is used as follows:
FileStream fileStream = new FileStream("data.txt", FileMode.Open); BinaryReader binaryReader = new BinaryReader(fileStream); while (binaryReader.PeekChar() != -1) { Console.WriteLine(binaryReader.ReadChar()); } binaryReader.Close();
In this code, the PeekChar method is used to determine when to stop reading values in the file. This will prevent a costly EndOfStreamException from being thrown by the ReadChar method if it tries to read past the end of the file.
Popularity: 2% [?]
RSS feed for comments on this post · TrackBack URI
Leave a reply