C#IO流_StreamReader和StreamWriter

ModelEngine·创作计划征文活动 10w+人浏览 1.4k人参与

*IO流_StreamReader和StreamWriter

  /*
     * Streamwriter 字符输出流
     * 在往文件中写入字符数据时,需要手动刷新缓冲区,实现把存入到缓冲区的数据写入到文件中
     * 或者我们可以选择直接释放流所占用的内存,也可以实现把缓冲区中的数据写入到文件中。
     */
    internal class Program
    {
        //定义一个文件路径
        static string filePath = "E:/桌面/user.txt";

        //定义一个方法:往文件中写入信息
        public static void WriteData()
        {
            //创建一个StreamWriter对象
            //new StreamWriter(string path, bool append, Encoding encoding)
            //带有三个参数的构造方法
            //第一个参数: 文件路径
            //第二个参数: 是否以追加的形式, append: true 表示追加写入
            //第三个参数: 编码字符集

            //using()代码块
            using (StreamWriter sw = new StreamWriter(filePath, true, Encoding.UTF8))
            {
                string str = "明天是星期五";

                //Write(string)	写入字符串(不自动换行)
                //WriteLine(string)  写入字符串并追加换行符 
                sw.WriteLine(str);

                //强制刷新缓冲区,从而把字符数据写入文件中
                //sw.Flush();

                //释放在内存打开的对象(StreamWriter)流
                //sw.Dispose();

                //关闭当前打开的对象(StreamWriter)流
                //sw.Close();
            }
        }

        //定义一个方法:读取文件中的字符信息
        public static void ReadData()
        {
            using (StreamReader sr = new StreamReader(filePath, Encoding.UTF8))
            {
                //Read() 读取单个字符
                //ReadToEnd() 读取流中所有剩余文本
                //string str = sr.ReadToEnd();

                //ReadLine()  读取一行文本(不含换行符)
                /*string text = "";
                while (true)
                {
                    string str = sr.ReadLine();
                    //退出循环
                    if (str == null)
                    { break; }
                    text += str + "\n";
                }
                Console.WriteLine(text);*/
            }
        }

        //注册功能
        public static void Reaister(string name, string id)
        {
            //写入到指定文件中   字符输出流 Streamwriter
            string user = name + "?" + id;
            using (StreamWriter ssw = new StreamWriter(filePath, true, Encoding.UTF8))
            {
                ssw.WriteLine(user);
            }
            Console.WriteLine("注册成功");
            //提示去登陆
            Console.WriteLine("请去登陆");
            Login(name, id);
        }
        //登录
        public static void Login(string name, string id)
        {
            //检验一下文件是否存在
            if (!File.Exists(filePath))
            {
                Console.WriteLine("文件不存在");
                //创建文件
                using (File.Create(filePath)) { }
                return;
            }
            // 第一步:把文件中的数据读取出来--方法到容器中
            // File类当中有没有一个方法:ReadAllLines()
            //string[] lines = File.ReadAllLines(filePath);
            // 定义一个布尔变量
            bool flag = false;
            using (StreamReader ssr = new StreamReader(filePath, Encoding.UTF8))
            {
                while (true)
                {
                    string str = ssr.ReadLine();
                    //退出循环
                    if (str == null) { break; }

                    //切割
                    string[] user = str.Split('?');
                    // 根据输入的用户名和密码来判断是否在容器中存在
                    // 为空校验
                    if (name == null || name == "" || id == null || id == "")
                    {
                        Console.WriteLine("用户名或者密码不能为空");
                        return;
                    }
                    //校验
                    // 第一个数据是用户名,第二个数据是密码
                    if (user[0].Equals(name) && user[1].Equals(id))
                    {
                        flag = true;
                        Console.WriteLine("登录成功");
                        break;
                    }
                }
            }
            if (!flag)
            {
                Console.WriteLine("登录失败");
                Console.WriteLine("请去注册!");
                // 调用一下注册
                Reaister(name, id);
            }
        }

        static void Main(string[] args)
        {
            //WriteData();//写入
            //ReadData(); //读取
            Console.WriteLine("请输入用户名");
            string name = Console.ReadLine();
            Console.WriteLine("请输入密码");
            string id = Console.ReadLine();
            Login(name, id);
        }

    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值