As described above, we can also redirect standard data streams when performing read/write operations on files. The redirection operators "<" and ">" are provided by the operating system. Therefore, if the operating system supports I/O redirection, You can redirect standard input and output without changing programs. In addition, the console class provides three redirection Methods: console. setin, console. setout, and console. seterror. Their function definitions are as follows:
Static void setin (textreader newin );
Static void setout (textwriter newout );
Static void seterror (textwriter newerror );
For example:
Streamwriter log_out = new streamwriter ("log.txt ");
Console. setout (log_out );
Console. writeline ("this is the start of Log File ");
At this time, we will find that the output is not displayed on the console, but is output to the log.txt text file we specified.
So far, whether we read files based on characters or byte, they are all accessed in order. However, C # provides us with a method to read files randomly. The seek () method is defined in the filestream class. The function prototype is as follows:
Long seek (long offset, seekorigin origin );
Here, offset refers to the offset byte after the specified position of the origin, and seekorigin is an enumeration type. Its value and meaning are as follows:
Seekorifin Enumeration
| Value |
Description |
| Seekorigin. Begin |
Start searching at the beginning of the file |
| Seekorigin. Current |
Search from the current file location |
| Seekorigin. End |
Search from the end of the file |
If an error occurs during the search process, an ioexception is thrown. If the underlying data stream does not support location search, a notsupportedexception exception is thrown.
In addition to the positioning method described above, we can also use the position attribute of the stream class. The position attribute is a read/write attribute that can obtain the current position or change the current position.
We have introduced two character-based data streams streamreader and streamwriter that implement the textreader and textwriter classes. Next we will introduce the other two data streams.
When performing memory-based I/O operations, it is much easier to use strings than to use byte arrays as the underlying storage. In this case, we can use the stringreader and stringwriter classes. Stringreader inherits the textreader class, And stringwriter inherits the textwriter class. They can use methods defined by the base class.
The stringreader class constructor is as follows:
Stringreader (string S );
S is the string to be initialized.
Stringwriter class constructor:
Stringwriter ();
Stringwriter (stringbuilder SB );
Stringwriter (iformatprovider formatprovider );
Stringwriter (stringbuilder Sb, iformatprovider formatprovider );
The first constringwriter creates an empty stringwriter object, and the second class initializes a stringwriter object with a stringbuilder object.
In addition to the methods described above for reading files, the file class also provides a lot of static methods for file operations, including file creation, copying, and moving, delete, encrypt, decrypt, and set and modify the file access time. These operations are easier.