This article illustrates how C # reads and writes text files. Share to everyone for your reference. The specific analysis is as follows:
The classes in the System.IO namespace provide file and other forms of input and output for managed applications. The basic widget for managed I/O is the stream, and the stream is the abstract representation of the byte-oriented data. The stream is represented by the System.IO.Stream class.
System.IO.FileStream allows the file to be accessed as a stream;
System.IO.MemoryStream allows the memory block to be accessed as a stream
The following is an example of a read-write file
Reference Namespaces First
Using System.IO;
The following is the source code
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80-81 |
namespace text File Open test {public partial class Form1:form {public Form1 () {initializecom Ponent (); } private void Btn_read_click (object sender, EventArgs e) {//anomaly detection start try {FileStream fs = new FileStream (@tB_PachFileName . Text, FileMode.Open, FileAccess.Read)//Read file settings StreamReader M_streamreader = new StreamReader (FS, System.Text.Encoding.GetEncoding ("GB2312"))//setting read/write encoding///using StreamReader class for reading file M_streamReader.BaseStream.Seek (0, Seekorigin.begin); Reads each row from the data stream until the last line of the file and displays the content in the Rtb_display.text this.rTB_Display.Text = ""; String strLine = M_streamreader.readline (); while (strLine!= null) {This.rTB_Display.Text + = StrLine + "n"; strLine = M_streamreader.readline ();}//close this StreamReader Object M_streamreader.close (); Catch {//Throw exception MessageBox.Show ("Specify file does not exist"); return;}//anomaly detection end} private void Btn_replace_click (object sender, EventArgs e) {//Judgment replacement start if (Tb_replace.text = "" &&tb_replace_2.text== "") {MessageBox.Show ("replace the characters you want to change Ah, you are too talented");} else { If (Rtb_display.text = "") {MessageBox.Show ("file content is empty cannot be replaced, check file");} else {string str = rTB_Display.Text.ToString (); rtb_display.text = str. Replace (@tB_Replace. Text, @tB_Replace_2. text);//replace}//end} private void Btn_save_click (object sender, EventArgs e) {// Anomaly detection starts try {//creates a file stream to write or create a streamwriter FileStream fs = new FileStream (@tB_Save. Text, FileMode.OpenOrCreate, FileAccess.Write); StreamWriter m_streamwriter = new StreamWriter (FS); M_streamwriter.flush (); Write content M_streamWriter.BaseStream.Seek (0, Seekorigin.begin) using StreamWriter files; Writes the contents of the RichTextBox1 to the file M_streamwriter.write (rtb_display.text); Close this file M_streamwriter.flush (); M_streamwriter.close (); Catch {//Throw exception MessageBox.Show ("Failed to write file, check path file name and Permissions compliant");}//Exception detection end}} |
I hope this article will help you with your C # programming.