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");
}