using System; using System.IO; class Test { public static void Main() { string path = @"c:\temp\MyTest.txt"; try { if (File.Exists(path)) { File.Delete(path); } using (StreamWriter sw = new StreamWriter(path)) { sw.WriteLine("This"); sw.WriteLine("is some text"); sw.WriteLine("to test"); sw.WriteLine("Reading"); } using (FileStream fs = new FileStream(path, FileMode.Open)) { using (StreamReader sr = new StreamReader(fs)) { while (sr.Peek() >= 0) { Console.WriteLine(sr.ReadLine()); } } } } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } } }
C#中StreamWriter应用实例
最新推荐文章于 2024-07-29 13:13:17 发布
本文介绍如何使用C#编程语言创建并写入一个简单的文本文件,然后通过读取操作来验证文件内容是否正确加载。通过实例演示了文件的创建、写入与读取过程,为开发者提供了一种基础的文件操作方法。
4547

被折叠的 条评论
为什么被折叠?



