C#读写txt文件的方法
2012-08-17 11:53
393 查看
1. 读取文件(StreamReader)
添加引用:using System.IO;
StreamReader objReader = new StreamReader(文件路径); string sLine=""; ArrayList LineList = new ArrayList(); while (sLine != null) { sLine = objReader.ReadLine(); if (sLine != null&&!sLine.Equals("")) LineList.Add(sLine); } objReader.Close(); return LineList;
常用:
StreamReader sr = new StreamReader("TestFile.txt")
//StreamReader sr = new StreamReader("TestFile.txt",Encoding.GetEncoding("GB2312")) //GBK String line; while ((line = sr.ReadLine()) != null) { textBox1 .Text +=ii.ToString ()+" -"+line.ToString()+"\r\n"; }
2. 写入文件(StreamWriter)
FileStream fs = new FileStream(文件路径, FileMode.Create); StreamWriter sw = new StreamWriter(fs); //开始写入 sw.Write(String); //清空缓冲区 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#读写txt文件的两种方法介绍[转]
- C#读写txt文件的两种方法介绍
- C#读写txt文件的两种方法介绍
- C#读写txt文件的两种方法介绍
- C#读写txt文件的两种方法介绍
- C#读写txt文件的两种方法介绍
- C#读写txt文件的两种方法介绍
- C#读写txt文件的两种方法介绍
- C#读写txt文件数据的两种方法
- C#读写txt文件的2种方法