当前位置: 首页>>代码示例>>C#>>正文


C# StreamWriter类代码示例

本文整理汇总了C#中System.IO.StreamWriter的典型用法代码示例。如果您正苦于以下问题:C# StreamWriter类的具体用法?C# StreamWriter怎么用?C# StreamWriter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


StreamWriter类属于System.IO命名空间,在下文中一共展示了StreamWriter类的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

//引入命名空间
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace StreamReadWrite
{
    class Program
    {
        static void Main(string[] args)
        {
            // Get the directories currently on the C drive.
            DirectoryInfo[] cDirs = new DirectoryInfo(@"c:\").GetDirectories();

            // Write each directory name to a file.
            using (StreamWriter sw = new StreamWriter("CDriveDirs.txt"))
            {
                foreach (DirectoryInfo dir in cDirs)
                {
                    sw.WriteLine(dir.Name);
                }
            }

            // Read and show each line from the file.
            string line = "";
            using (StreamReader sr = new StreamReader("CDriveDirs.txt"))
            {
                while ((line = sr.ReadLine()) != null)
                {
                    Console.WriteLine(line);
                }
            }
        }
    }
}
开发者ID:.NET开发者,项目名称:System.IO,代码行数:37,代码来源:StreamWriter

示例2: new StreamWriter(String filePath)

//引入命名空间
using System;
using System.IO;


public class MyStreamWriterReader {
    static void Main(string[] args) {
        StreamWriter writer = new StreamWriter("reminders.txt");
        writer.WriteLine("...");
        writer.WriteLine("Don't forget these numbers:");
        for (int i = 0; i < 10; i++)
            writer.Write(i + " ");

        writer.Write(writer.NewLine);

        writer.Close();
        StreamReader sr = new StreamReader("reminders.txt");
        string input = null;
        while ((input = sr.ReadLine()) != null) {
            Console.WriteLine(input);
        }
    }
}
开发者ID:C#程序员,项目名称:System.IO,代码行数:23,代码来源:StreamWriter

示例3: new StreamWriter(fs, Encoding.UTF8)

//引入命名空间
using System;
using System.IO;
using System.Text;

class MainClass {
    static void Main() {
        using (FileStream fs = new FileStream("test.txt", FileMode.Create)) {
            using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8)) {
                w.WriteLine(124.23M);
                w.WriteLine("Test string");
                w.WriteLine('!');
            }
        }
        using (FileStream fs = new FileStream("test.txt", FileMode.Open)) {
            using (StreamReader r = new StreamReader(fs, Encoding.UTF8)) {
                Console.WriteLine(Decimal.Parse(r.ReadLine()));
                Console.WriteLine(r.ReadLine());
                Console.WriteLine(Char.Parse(r.ReadLine()));
            }
        }
    }
}
开发者ID:C#程序员,项目名称:System.IO,代码行数:23,代码来源:StreamWriter

示例4: new StreamWriter

//引入命名空间
using System;
using System.IO;
using System.Text;

class MainClass {
    static void Main() {
        using (FileStream fs = new FileStream("test.txt", FileMode.Create)) {
            using (StreamWriter w = new StreamWriter(fs, Encoding.UTF8)) {
                w.WriteLine(124.23M);
                w.WriteLine("Test string");
                w.WriteLine('!');
            }
        }
        Console.WriteLine("Press Enter to read the information.");
        Console.ReadLine();
        using (FileStream fs = new FileStream("test.txt", FileMode.Open)) {
            using (StreamReader r = new StreamReader(fs, Encoding.UTF8)) {
                Console.WriteLine(Decimal.Parse(r.ReadLine()));
                Console.WriteLine(r.ReadLine());
                Console.WriteLine(Char.Parse(r.ReadLine()));
            }
        }
    }
}
开发者ID:C#程序员,项目名称:System.IO,代码行数:25,代码来源:StreamWriter


注:本文中的System.IO.StreamWriter类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。