C#笔记—文本读写

C#笔记1## C#笔记—文本读写

using System;
using System.IO;

public class Duxie
{
    public double[,] input_matrix()
    {
        double[,] input_matrix = new double[4, 4];
        for (int i = 0; i < 4; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                input_matrix[i, j] = Convert.ToDouble(Console.ReadLine());
            }
        }
        return input_matrix;
    }
    public void Writeto(double[,] arr)             //保存txt文本
    {
        //获取当前时间 年月日时分秒  大写的MM是月,小写的mm是分钟 ss是秒
        string name = DateTime.Now.ToString("yyyy-MM-dd-hh-mm-ss");
        //保存地址为D盘,文件名根据系统时间自创
        FileStream fs = new FileStream("D:\\C#_text\\" + name + ".txt", FileMode.Create);
        StreamWriter sw = new StreamWriter(fs);
        //初始化二维数组s 用来接收arr
        double[,] s = new double[4, 4];
        //接收arr
        s = arr;
        for (int l = 0; l < 4; ++l)
        {
            for (int h = 0; h < 4; ++h)
            {
                //s的每个值和arr的每个值对应
                s[l, h] = arr[l, h];

                int output;

                output = Convert.ToInt32(s[l, h]);
                //有个空格作为间隔,
                sw.Write(output + " ");
            }
            sw.WriteLine();
        }
        //清空缓冲区
        sw.Flush();
        //关闭流
        sw.Close();
        fs.Close();
    }

    public double[,] Readin()             //保存txt文本
    {
        int counter = 0;
        string line;

        // Read the file and display it line by line.  
        //FileStream fs = new FileStream("D:\\C#_text\\" + name + ".txt", FileMode.Create);
       // StreamWriter sw = new StreamWriter(fs);

        StreamReader file = new StreamReader(@"D:\C#_text\matrix_3_3.txt");

        double[,] matrix = new double[4, 4];
        while ((line = file.ReadLine()) != null)
        {
            string[] line1 = System.Text.RegularExpressions.Regex.Replace(line.Trim(), @"[\s]+", " ").Split(" ".ToCharArray());



            matrix[counter, 0] = Convert.ToDouble(line1[0]);
            matrix[counter, 1] = Convert.ToDouble(line1[1]);
            matrix[counter, 2] = Convert.ToDouble(line1[2]);
            matrix[counter, 3] = Convert.ToDouble(line1[3]);
            for (int i = 0; i < 4; i++)
                Console.Write(matrix[counter, i] + " ");
            Console.WriteLine();

            // System.Console.WriteLine(line);
            counter++;
        }

        file.Close();
        Console.WriteLine("There were {0} lines.", counter);
        // Suspend the screen.  
        //Console.ReadLine();   如果加上这句则需要再读入一个输入才能跳出该程序转入下一程序
        return matrix;
    }
}


class WriteTextFile
{
    static void Main()
    {
        Duxie mani = new Duxie();
        //string path= "matrix_3_3.txt";
        double[,] goal = mani.Readin();
        mani.Writeto(goal);
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值