码迷,mamicode.com
首页 > 编程语言 > 详细

c#中对txt文件的读取与写入,针对二维数组

时间:2016-11-27 19:04:49      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:writer   readline   关闭   row   create   code   mod   二维   for   

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();
        }
    }

 

c#中对txt文件的读取与写入,针对二维数组

标签:writer   readline   关闭   row   create   code   mod   二维   for   

原文地址:http://www.cnblogs.com/lili-work/p/6106670.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!