zoukankan      html  css  js  c++  java
  • C# list和文本文件转换

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

            }

  • 相关阅读:
    2020牛客暑期多校(三)
    贪心算法
    高级搜索题集
    状态压缩dp
    [kuangbin带你飞]专题二 搜索进阶
    [kuangbin带你飞]专题一 简单搜索
    HDU 1695 GCD(求两区间的互质数对+容斥原理)
    UVA 10200 Prime Time(简单素数判定预处理)
    HDU 1452 FZU 1053 Happy 2004(逆元函数+因子和函数+大指数取模化简公式)
    低三位的数是8的倍数就可以被8整除
  • 原文地址:https://www.cnblogs.com/gisoracle/p/1576567.html
Copyright © 2011-2022 走看看