zoukankan      html  css  js  c++  java
  • 30天C#基础巩固------读写流(StreamWrite/StreamReader)

    一:读写流的一些案例。

    --->关于StreamWrite  

        这里的一些常用的方法和我们之前的那个FileStream是一样的,参数很多都是一样的用法。

                Console.WriteLine("输入文件名:");
                string file = Console.ReadLine();
                //StreamWriter------------实现一个 System.IO.TextWriter,使其以一种特定的编码向流中写入字符。
                using (StreamWriter writer = new StreamWriter(file, true, Encoding.Default))      
                {
                     //写文本
                    while (true)
                    {
                        Console.Write("AHUI:user/ahui>");
                        string input = Console.ReadLine();
                        if (input.ToLower()=="exit")
                        {
                            Console.WriteLine("保存成功");
                            Console.ReadKey();
                            return;
                        }
                        //WriteLineAsync------------将后跟行结束符的字符串异步写入该流。
                        writer.WriteLineAsync(input);         //这里就把我们输入的内容全部写入到我们指定的文件中了。
                    }
                }

    image

    下面是结果。

    imageimage

    --->关于StreamRead

                Console.WriteLine("输入文件名:");
                string file = Console.ReadLine();
                //StreamWriter------------实现一个 System.IO.TextWriter,使其以一种特定的编码向流中写入字符。
                using (StreamReader reader= new StreamReader(file,Encoding.Default))
                {
                    //读取之前我们写的文本
                    //通过循环的方式来按行读取
                    string str = null;
                    int i = 0;
                    //reader.ReadLine()-------从当前流中读取一行字符并将数据作为字符串返回。
                    while ((str = reader.ReadLine()) != null)        
                    {
                        Console.WriteLine("{0}	{1}",++i,str);
                    }
                    Console.ReadKey();
                }

    image

  • 相关阅读:
    谈谈对 ”框架“ 这个概念的理解,以及它和库的区别
    npm 安装或更新模块失败的解决办法
    vs 2017/2015/2013 如何定位C++内存泄漏
    django 在python 3中提示 无法找到 MySQLDB
    合并表中数据
    pymysql
    mysql-错误备查
    tensorflow-gpu 使用的常见错误
    Ubuntu 安装 tensorflow-gpu + keras
    mysql 查看表结构方法
  • 原文地址:https://www.cnblogs.com/netxiaohui/p/5529567.html
Copyright © 2011-2022 走看看