- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace readfile
- {
- class Program
- {
- static void Main(string[] args)
- {
- string[] lines = System.IO.File.ReadAllLines(
- @"f:/test.txt", Encoding.Default); //@後面是文件的路徑
- //讀入的每一行都做爲數組的一個元素
- // foreach (string item in lines)
- // Console.WriteLine(item);
- int maxscore = -1;
- string maxname = "";
- string maxage = "";
- for (int i = 0; i < lines.Length; i++)
- {
- string line = lines[i]; //將字符串數組轉換爲字符串
- string[] str = line.Split('|'); //Split 是將字符串轉換爲字符串數組
- string name = str[0];
- string age = str[1];
- string score = str[2];
- int score_num = Convert.ToInt32(score);
- if (score_num > maxscore)
- {
- maxscore = score_num;
- maxage = age;
- maxname = name;
- }
- }
- Console.WriteLine("最高分的的名字是:{0}", maxname);
- Console.WriteLine("最高分的的年齡是:{0}", maxage);
- Console.WriteLine("最高分的的分數是:{0}", maxscore);
- Console.ReadKey();
- }
- }
- }
文件內容的讀取
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.