乡下人产国偷v产偷v自拍,国产午夜片在线观看,婷婷成人亚洲综合国产麻豆,久久综合给合久久狠狠狠9

  • <output id="e9wm2"></output>
    <s id="e9wm2"><nobr id="e9wm2"><ins id="e9wm2"></ins></nobr></s>

    • 分享

      C#讀取文本文件和C# 寫文本文件

       Cloud書屋 2013-01-31

      C#讀取文本文件

      今天一個學生問我如何從一個文本中讀取內容,如下是做的是控制臺中的例子,在別的地方也是這個道理。


              // 讀操作
              public static void Read()
              {
                  // 讀取文件的源路徑及其讀取流
                  string strReadFilePath = @"..\..\data\ReadLog.txt";
                  StreamReader srReadFile = new StreamReader(strReadFilePath);

                  // 讀取流直至文件末尾結束
                  while (!srReadFile.EndOfStream)
                  {
                      string strReadLine = srReadFile.ReadLine(); //讀取每行數(shù)據
                      Console.WriteLine(strReadLine); //屏幕打印每行數(shù)據
                  }

                  // 關閉讀取流文件
                  srReadFile.Close();
                  Console.ReadKey();
              }

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

      C# 寫文本文件

              // 寫操作
              public static void Write()
              {
                  // 統(tǒng)計寫入(讀取的行數(shù))
                  int WriteRows = 0;

                  // 讀取文件的源路徑及其讀取流
                  string strReadFilePath = @"..\..\data\ReadLog.txt";
                  StreamReader srReadFile = new StreamReader(strReadFilePath);

                  // 寫入文件的源路徑及其寫入流
                  string strWriteFilePath = @"..\..\data\WriteLog.txt";
                  StreamWriter swWriteFile = File.CreateText(strWriteFilePath);

                  // 讀取流直至文件末尾結束,并逐行寫入另一文件內
                  while (!srReadFile.EndOfStream)
                  {
                      string strReadLine = srReadFile.ReadLine(); //讀取每行數(shù)據
                      ++WriteRows; //統(tǒng)計寫入(讀?。┑臄?shù)據行數(shù)

                      swWriteFile.WriteLine(strReadLine); //寫入讀取的每行數(shù)據
                      Console.WriteLine("正在寫入... " + strReadLine);
                  }

                  // 關閉流文件
                  srReadFile.Close();
                  swWriteFile.Close();

                  Console.WriteLine("共計寫入記錄總數(shù):" + WriteRows);
                  Console.ReadKey();
              }

       

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

      完整源代碼(經過本人測試,直接運行就可)

      using System;
      using System.Collections.Generic;
      using System.Linq;
      using System.Text;

      using System.IO; // 引用輸入輸出操作的命令空間

      namespace ReadWriteFile
      {
          class Program
          {

              // 主函數(shù)
              static void Main(string[] args)
              {
                  Read(); // 讀操作

                  Write(); // 寫操作
              }

              // 讀操作
              public static void Read()
              {
                  // 讀取文件的源路徑及其讀取流
                  string strReadFilePath = @"..\..\data\ReadLog.txt";
                  StreamReader srReadFile = new StreamReader(strReadFilePath);

                  // 讀取流直至文件末尾結束
                  while (!srReadFile.EndOfStream)
                  {
                      string strReadLine = srReadFile.ReadLine(); //讀取每行數(shù)據
                      Console.WriteLine(strReadLine); //屏幕打印每行數(shù)據
                  }

                  // 關閉讀取流文件
                  srReadFile.Close();
                  Console.ReadKey();
              }

              // 寫操作
              public static void Write()
              {
                  // 統(tǒng)計寫入(讀取的行數(shù))
                  int WriteRows = 0;

                  // 讀取文件的源路徑及其讀取流
                  string strReadFilePath = @"..\..\data\ReadLog.txt";
                  StreamReader srReadFile = new StreamReader(strReadFilePath);

                  // 寫入文件的源路徑及其寫入流
                  string strWriteFilePath = @"..\..\data\WriteLog.txt";
                  StreamWriter swWriteFile = File.CreateText(strWriteFilePath);

                  // 讀取流直至文件末尾結束,并逐行寫入另一文件內
                  while (!srReadFile.EndOfStream)
                  {
                      string strReadLine = srReadFile.ReadLine(); //讀取每行數(shù)據
                      ++WriteRows; //統(tǒng)計寫入(讀取)的數(shù)據行數(shù)

                      swWriteFile.WriteLine(strReadLine); //寫入讀取的每行數(shù)據
                      Console.WriteLine("正在寫入... " + strReadLine);
                  }

                  // 關閉流文件
                  srReadFile.Close();
                  swWriteFile.Close();

                  Console.WriteLine("共計寫入記錄總數(shù):" + WriteRows);
                  Console.ReadKey();
              }
          }
      }

        本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權內容,請點擊一鍵舉報。
        轉藏 分享 獻花(0

        0條評論

        發(fā)表

        請遵守用戶 評論公約

        類似文章 更多