I had an idea earlier, writing a small program that can read documents and find and replace keywords. It is actually very simple. Just recently I saw the file system chapter, this afternoon, I just made one. Here is a summary:
1. Read text files with streamreader and encode them with encoding. default.
Streamreader sr = new streamreader (filepath, encoding. Default); rtbcontent. Text = Sr. readtoend (); Sr. Close (); // release locked Resources
2. Count the characters to be searched. The string. indexof () and for loop conditions are used.
String content = rtbcontent. text. trim (); string key = txtinput. text. trim (); int COUNT = 0; int start; For (START = 0; (START = content. indexof (Key, start)>-1; Start = start + key. length)
{Count ++;} MessageBox. Show ("" + key + "" appears "+ count. tostring () +" times ");
Network Disk address for this applet: http://pan.baidu.com/s/1hqAAZpM
-------------------------------- Summary completed -------------------------------------------------------------------------------------------
1. readall and writeall methods of the file class (. NET Framework 2.0)
2. Concept of stream:
A stream is an object used to transmit data. Data transmission has two directions:
If the data is transmitted from the external source to the program, this is the read stream; if the data is transmitted from the program to the external source during the summer vacation, This is the write stream.
3. filestream class: used to read and write binary files. To construct a filestream instance, you need the following four pieces of information: (. NET Framework 1.x, which is used before the release of 2.0)
File to be accessed
Indicates how to open the file mode-filemode
File Access Method-fileaccess
Shared access-fileshare
4. read/write streamreader and streamwriter
You can associate streamreader with filestream:
Filestream FS = new filestream ("C: \ readme.txt", filemode. Open, fileaccess. Read, fileshare. None );
Streamreader sr = new streamreader (FS );
Obtain streamreader from the fileinfo instance
Fileinfo myfile = new fileinfo ("C: \ readme.txt ");
Streamreader sr = myfile. opentext ();