C # list and text file conversion

Source: Internet
Author: User

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); // Chinese Garbled Text with System. Text. Encoding. Default, or System. Text. Encoding. GetEncoding ("GB2312 ")
// Use the StreamReader class to read files
M_streamReader.BaseStream.Seek (0, SeekOrigin. Begin );
// Read each row from the data stream until the last row of the file is displayed in richTextBox1.

String strLine = m_streamReader.ReadLine ();
While (strLine! = Null)
{
List. Add (strLine );
StrLine = m_streamReader.ReadLine ();
}
// Close this StreamReader object
M_streamReader.Close ();
Return list;

}
Private void writeListToFile (List <string> pList, string myFileName)
{
// Create a file stream for writing or creating a StreamWriter
System. IO. FileStream fs = new System. IO. FileStream (myFileName, FileMode. OpenOrCreate, FileAccess. Write );
StreamWriter m_streamWriter = new StreamWriter (fs );
M_streamWriter.Flush ();
// Use StreamWriter to write content to files
M_streamWriter.BaseStream.Seek (0, SeekOrigin. Begin );
// Write the content in richTextBox1 to the file
For (int I = 0; I <pList. Count; I ++)
{

M_streamWriter.WriteLine (pList [I]);
}
// Close this file
M_streamWriter.Flush ();
M_streamWriter.Close ();

}
 

// Test by gisoracle 2009.9.28
Private void button2_Click (object sender, EventArgs e)
{
List <string> slist = readFileToList (@ "C: conversion \ 54.txt ");
WriteListToFile (slist, @ "C: conversion \ 541.txt ");

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.