文件內容的讀取

Code:
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5.   
  6. namespace readfile  
  7. {  
  8.     class Program  
  9.     {  
  10.         static void Main(string[] args)  
  11.         {  
  12.   
  13.             string[] lines = System.IO.File.ReadAllLines(  
  14.                 @"f:/test.txt", Encoding.Default);   //@後面是文件的路徑  
  15.                                                      //讀入的每一行都做爲數組的一個元素  
  16.            // foreach (string item in lines)  
  17.              //   Console.WriteLine(item);  
  18.             int maxscore = -1;  
  19.             string maxname = "";  
  20.             string maxage = "";  
  21.             for (int i = 0; i < lines.Length; i++)  
  22.             {  
  23.                 string line = lines[i];   //將字符串數組轉換爲字符串  
  24.                 string[] str = line.Split('|');  //Split 是將字符串轉換爲字符串數組  
  25.                 string name = str[0];  
  26.                 string age = str[1];  
  27.                 string score = str[2];  
  28.                 int score_num = Convert.ToInt32(score);  
  29.                 if (score_num > maxscore)  
  30.                 {  
  31.                     maxscore = score_num;  
  32.                     maxage = age;  
  33.                     maxname = name;  
  34.                 }  
  35.             }  
  36.             Console.WriteLine("最高分的的名字是:{0}", maxname);  
  37.             Console.WriteLine("最高分的的年齡是:{0}", maxage);  
  38.             Console.WriteLine("最高分的的分數是:{0}", maxscore);  
  39.   
  40.             Console.ReadKey();  
  41.   
  42.         }  
  43.     }  
  44. }  


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章