private List<string> readFileToList(string fileName)
{
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
List<string> list = new List<string>();
StreamReader m_streamReader = new StreamReader(fs);//中文乱码加上System.Text.Encoding.Default,或则System.Text.Encoding.GetEncoding("GB2312")
//使用StreamReader类来读取文件
m_streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
// 从数据流中读取每一行,直到文件的最后一行,并在richTextBox1中显示出内容
string strLine = m_streamReader.ReadLine();
while (strLine != null)
{
list.Add(strLine);
strLine = m_streamReader.ReadLine();
}
//关闭此StreamReader对象
m_streamReader.Close();
return list;
}
private void writeListToFile(List<string> pList, string myFileName)
{
//创建一个文件流,用以写入或者创建一个StreamWriter
System.IO.FileStream fs = new System.IO.FileStream(myFileName, FileMode.OpenOrCreate, FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fs);
m_streamWriter.Flush();
// 使用StreamWriter来往文件中写入内容
m_streamWriter.BaseStream.Seek(0, SeekOrigin.Begin);
// 把richTextBox1中的内容写入文件
for (int i = 0; i < pList.Count; i++)
{
m_streamWriter.WriteLine(pList[i]);
}
//关闭此文件
m_streamWriter.Flush();
m_streamWriter.Close();
}
//测试 by gisoracle 2009.9.28
private void button2_Click(object sender, EventArgs e)
{
List<string> slist = readFileToList(@"C:\转换\54.txt");
writeListToFile(slist, @"C:\转换\541.txt");
}