国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C# > 正文

Windows系統中使用C#讀取文本文件內容的小示例

2020-01-24 01:17:16
字體:
來源:轉載
供稿:網友

讀取文本文件中的內容

此示例讀取文本文件的內容以使用 System.IO.File 選件類的靜態方法 ReadAllText 和 ReadAllLines。

class ReadFromFile{  static void Main()  {    // The files used in this example are created in the topic    // How to: Write to a Text File. You can change the path and    // file name to substitute text files of your own.    // Example #1    // Read the file as one string.    string text = System.IO.File.ReadAllText(@"C:/Users/Public/TestFolder/WriteText.txt");    // Display the file contents to the console. Variable text is a string.    System.Console.WriteLine("Contents of WriteText.txt = {0}", text);    // Example #2    // Read each line of the file into a string array. Each element    // of the array is one line of the file.    string[] lines = System.IO.File.ReadAllLines(@"C:/Users/Public/TestFolder/WriteLines2.txt");    // Display the file contents by using a foreach loop.    System.Console.WriteLine("Contents of WriteLines2.txt = ");    foreach (string line in lines)    {      // Use a tab to indent each line of the file.      Console.WriteLine("/t" + line);    }    // Keep the console window open in debug mode.    Console.WriteLine("Press any key to exit.");    System.Console.ReadKey();  }}

一次一行地讀取文本文件
本示例使用 StreamReader 類的 ReadLine 方法將文本文件的內容讀取(一次讀取一行)到字符串中。所有文本行都保存在字符串 line 中并顯示在屏幕上。

int counter = 0;string line;// Read the file and display it line by line.System.IO.StreamReader file =   new System.IO.StreamReader(@"c:/test.txt");while((line = file.ReadLine()) != null){  System.Console.WriteLine (line);  counter++;}file.Close();System.Console.WriteLine("There were {0} lines.", counter);// Suspend the screen.System.Console.ReadLine();

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 东海县| 娄烦县| 崇礼县| 新绛县| 图木舒克市| 长岭县| 卫辉市| 永济市| 吴川市| 武城县| 浦县| 连江县| 尼木县| 富裕县| 平凉市| 姚安县| 莫力| 兴山县| 潜山县| 景德镇市| 宁化县| 乃东县| 沛县| 宜昌市| 合水县| 滁州市| 平泉县| 闵行区| 南澳县| 徐闻县| 长治县| 桃源县| 满城县| 汤原县| 东阿县| 仲巴县| 永定县| 太保市| 循化| 买车| 山东|