03-StreamReader和StreamWriter的案例

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

namespace _06職工工資文件翻倍練習
{
    class Program
    {
        static void Main(string[] args)
        {
            //1.讀取salary1.txt
            using (StreamReader sr = new StreamReader("salary1.txt", Encoding.GetEncoding("gb2312")))
            {
                //建立一個寫入文本文件的文件流
                using (StreamWriter sw = new StreamWriter("salary2.txt"))
                {
                    string result;
                    while ((result = sr.ReadLine()) != null)
                    {//對每一行進行處理(工資翻倍)
                        string[] parts = result.Split('|'); //文件內容格式 hzy|8000
                        parts[1] = (Convert.ToInt32(parts[1]) * 2).ToString();

                        string newLine = string.Join("|", parts);
                        sw.WriteLine(newLine);//把newLine的內容寫入到文件中。
                    }
                }
            }
            Console.WriteLine("ok");
            Console.ReadKey();
        }
    }
}
相關文章
相關標籤/搜索