C#讀寫文字檔的範例

來源:互聯網
上載者:User

        private void button1_Click(object sender, EventArgs e)
        {
            //把檔案的內容讀入到RICHTEXTBOX中
            FileStream fs = new FileStream("d://1.txt", FileMode.Open, FileAccess.Read);
            StreamReader m_streamReader = new StreamReader(fs);

            //使用StreamReader類來讀取檔案
            m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
            // 從資料流中讀取每一行,直到檔案的最後一行,並在richTextBox1中顯示出內容
          this.richTextBox1.Text = "";
            string strLine = m_streamReader.ReadLine();
            while (strLine != null)
            {
                this.richTextBox1.Text += strLine + "/n";
                strLine = m_streamReader.ReadLine();
            }
            //關閉此StreamReader對象
            m_streamReader.Close();
        }

=============================================================

        private void button2_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream("d://1.txt", FileMode.OpenOrCreate, FileAccess.Write);
            StreamWriter m_streamWriter = new StreamWriter(fs);
            //m_streamWriter.Flush();
            //使用StreamWriter來往檔案中寫入內容
            m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);
            //把richTextBox1中的內容寫入檔案
            m_streamWriter.WriteLine (richTextBox1.Text);
            m_streamWriter.Flush();
            //關閉此檔案
            m_streamWriter.Close();
        }

 

=============================================================

 

        private void button3_Click(object sender, EventArgs e)
        {
            //詳解FILE類常用方法說明
            //http://venus.net.blog.163.com/blog/static/244652352007114540673/
           
            //關於字串前面加@的用法
            //http://apps.hi.baidu.com/share/detail/5632430
           
            //比較精簡的讀寫檔案的例子
            //File.ReadAllText或者File.ReadAllLines區別是
            //前者讀取所有內容放到一個string中
            //後者放到string數組中
            //前者帶斷行符號換行標記,後者不帶,
            //不過要帶上斷行符號換行標記的話,需要遍曆整個數組,
            //用stringbulder進行字串構造,直接用+的話,時間效率很低

            //第一種讀檔案方法
            string[] str = File.ReadAllLines(@"D:/1.txt");//用這個方法讀

            for (int i = 0; i <= str.Length - 1; i++)
            {
                richTextBox1.Text += str[i].ToString();
            }
            //第二種讀檔案方法
            string str1 = File.ReadAllText(@"D:/1.txt");//用這個方法讀
            richTextBox1.Text += str1;           
            //第一種寫檔案方法
            File.AppendAllText(@"D:/1.txt",richTextBox1.Text);//用這個方法寫入
            //第二種寫檔案方法
            File.WriteAllText(@"D:/1.txt", richTextBox1.Text);
        }

 

=============================================================

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.