c#中對txt文件的讀取與寫入,針對二維數組


class Program
    {
        public static int ROW = 5;
        public static int COL = 6;
        static string[,] str = new string[ROW, COL];

        static void Main(string[] args)
        {
            Read();
            Write();
        }
        public static void Read()
        {
            string[] a;
            StreamReader sr = new StreamReader("D:\\imx_2016-2017\\study\\001.txt", Encoding.Default);
            for (int i = 0; i < ROW; i++)
            {
                String line = sr.ReadLine();
                a = line.Split(',');
                for (int j = 0; j < COL; j++)
                {
                    str[i, j] = a[j];
                    //Console.Write(str[i, j]);
                    //Console.Write("\t");
                }
                //Console.WriteLine();
            }
            //Console.ReadKey();
        }
        public static void Write()
        {
            FileStream fs = new FileStream("D:\\imx_2016-2017\\study\\002.txt", FileMode.Create);
            StreamWriter sw = new StreamWriter(fs);
            for (int i = 0; i < ROW; i++)
            {
                for (int j = 0; j < COL; j++)
                {
                    sw.Write(str[i,j]);
                }
                sw.WriteLine();
            }
            //清空緩沖區
            sw.Flush();
            //關閉流
            sw.Close();
            fs.Close();
        }
    }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM