熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

C# list和文本文件轉換

2022-06-13   來源: .NET編程 

  private List<string> readFileToList(string fileName)

  {

  FileStream fs = new FileStream(fileName FileModeOpen FileAccessRead);

  List<string> list = new List<string>();

  StreamReader m_streamReader = new StreamReader(fs);//中文亂碼加上SystemTextEncodingDefault或則SystemTextEncodingGetEncoding(GB)

  //使用StreamReader類來讀取文件

  m_streamReaderBaseStreamSeek( SeekOriginBegin);

  // 從數據流中讀取每一行直到文件的最後一行並在richTextBox中顯示出內容

  string strLine = m_streamReaderReadLine();

  while (strLine != null)

  {

  listAdd(strLine);

  strLine = m_streamReaderReadLine();

  }

  //關閉此StreamReader對象

  m_streamReaderClose();

  return list;

  }

  private void writeListToFile(List<string> pList string myFileName)

  {

  //創建一個文件流用以寫入或者創建一個StreamWriter

  SystemIOFileStream fs = new SystemIOFileStream(myFileName FileModeOpenOrCreate FileAccessWrite);

  StreamWriter m_streamWriter = new StreamWriter(fs);

  m_streamWriterFlush();

  // 使用StreamWriter來往文件中寫入內容

  m_streamWriterBaseStreamSeek( SeekOriginBegin);

  // 把richTextBox中的內容寫入文件

  for (int i = ; i < pListCount; i++)

  {

  m_streamWriterWriteLine(pList[i]);

  }

  //關閉此文件

  m_streamWriterFlush();

  m_streamWriterClose();

  }

  //測試 by gisoracle

  private void button_Click(object sender EventArgs e)

  {

  List<string> slist = readFileToList(@C:\轉換\txt);

  writeListToFile(slist @C:\轉換\txt);

  }


From:http://tw.wingwit.com/Article/program/net/201311/13050.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.