C#读写txt文件
2014-11-18 15:15
218 查看
(1).使用StreamReader读取文件,然后一行一行的输出。 public void Read(string path) { StreamReader sr = new StreamReader(path,Encoding.Default); String line; while ((line = sr.ReadLine()) != null) { Console.WriteLine(line.ToString()); } } (2).使用FileStream类创建文件,然后将数据写入到文件里。 public void Write() { FileStream fs = new FileStream("E:\\ak.txt", FileMode.Create); //获得字节数组 byte[] data = System.Text.Encoding.Default.GetBytes("Hello World!"); //开始写入 fs.Write(data, 0, data.Length); //清空缓冲区、关闭流 fs.Flush(); fs.Close(); } (3).使用FileStream类创建文件,使用StreamWriter类,将数据写入到文件。 public void Write(string path) { FileStream fs = new FileStream(path, FileMode.Create); StreamWriter sw = new StreamWriter(fs); //开始写入 sw.Write("Hello World!!!!"); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); }
相关文章推荐
- [C#]解决读写包含汉字的txt文件时乱码的问题
- C#与TXT文件的读写操作
- C#读写txt文件
- C#读写txt文件多种方法实例代码
- C#解决读写包含汉字的txt文件时乱码的问题
- C# 如何创建与读写txt文件
- C#读写txt文件的两种方法
- [C#]解决读写包含汉字的txt文件时乱码的问题
- C#读写txt文件的两种方法介绍
- [C#]解决读写包含汉字的txt文件时乱码的问题
- c#用DLL,API读写删INI.TXT文件的例子
- C#解决读写包含汉字的txt文件时乱码的问题
- C#与TXT文件的读写操作
- C#读写txt文件的两种方法介绍
- c# TXT文件读写
- 转:[C#]解决读写包含汉字的txt文件时乱码的问题
- net中C#读写txt文件
- C#读写txt文件
- C#读写txt文件数据的两种方法
- C# 如何创建与读写txt文件