C#读写文本文件代码片段
2018-07-20 来源:open-open
读取文本文件代码片段
using System; using System.IO; public class TestReadFile { public static void Main(String[] args) { // Read text file C:\temp\test.txt FileStream fs = new FileStream(@c:\temp\test.txt , FileMode.Open, FileAccess.Read); StreamReader sr = new StreamReader(fs); String line=sr.ReadLine(); while (line!=null) { Console.WriteLine(line); line=sr.ReadLine(); } sr.Close(); fs.Close(); } }写文本文件代码片段
using System; using System.IO; public class TestWriteFile { public static void Main(String[] args) { // Create a text file C:\temp\test.txt FileStream fs = new FileStream(@c:\temp\test.txt , FileMode.OpenOrCreate, FileAccess.Write); StreamWriter sw = new StreamWriter(fs); // Write to the file using StreamWriter class sw.BaseStream.Seek(0, SeekOrigin.End); sw.WriteLine( First Line ); sw.WriteLine( Second Line); sw.Flush(); } }
标签: 代码
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点!
本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。
最新资讯
热门推荐