zoukankan      html  css  js  c++  java
  • StreamWriter 的应用

       StreamWriter 常用于向文本文件写字符串的, 默认的编码是 utf-8. 在 File.CreateText 方法说明中即有这样的例子:

          string path = @"c:\temp\MyTest.txt";
            if (!File.Exists(path))
            {
                // Create a file to write to.
                using (StreamWriter sw = File.CreateText(path))
                {
                    sw.WriteLine("Hello");
                    sw.WriteLine("And");
                    sw.WriteLine("Welcome");
                }
            }

    流指向开启的文本文件, 常用的方法就是 WriteLine, Write.

    而在其构造函数中, 可以更改编码格式, 如下一种构造函数:

    public StreamWriter(string path, bool append, Encoding encoding);
    这样的一个示例:
    StreamWriter sw = new StreamWriter(@"c:\test.txt", false, Encoding.Unicode);
    sw.WriteLine("写入的一行文本.");
    sw.Flush();
    sw.Close();

    ~做事情贵在坚持~
  • 相关阅读:
    改变字段的值
    创建新的对象
    根据方法的名称来执行方法
    获取类的字段
    获取构造器的信息
    找出类的方法
    开始使用Reflection
    反射简介
    leetcode501
    leetcode235
  • 原文地址:https://www.cnblogs.com/csMapx/p/2060973.html
Copyright © 2011-2022 走看看