zoukankan      html  css  js  c++  java
  • c# 读取txt文件并分隔

     public static List<PostPerson> GetNameByFile()
            {
                
                #region 读取txt文件
                var file = File.Open(Environment.CurrentDirectory + "\aaa.txt",  FileMode.Open);
                List<string> txt = new List<string>();
                using (var stream = new StreamReader(file, System.Text.Encoding.GetEncoding("gb2312")))
                {
                    while (!stream.EndOfStream)
                    {
                        txt.Add(stream.ReadLine());
                    }
                }
                file.Close();
                List<PostPerson> models = new List<PostPerson>();
                //循环list
                foreach (string item in txt)
                {
                    string[] tempstr = item.Split(',');
                    PostPerson model = new PostPerson();
                    model.pcode = tempstr[0];
                    model.pname= tempstr[1];
                    models.Add(model);
                }
                // var line = 0;
                //txt.ForEach(t => {
                //    var row = 0;
                //    t.Split(',').ToList().ForEach(p => {
                //        PostPerson model = new PostPerson();
                //        model.pcode = p;
                //        row++;
                //    });
                //    line++;
                //});
                return models;
                #endregion
            }

    txt文件中的格式:

    xxxxxx,xxxx
    xxxxxx,xxxx
    
  • 相关阅读:
    SpringRequestContext源码阅读
    MyBatis事务管理源码阅读
    linux查找依赖文件
    GitHub
    Qt Quick
    centos7下安装chrome
    软件使用
    排序算法之冒泡排序
    c++学习
    cent6.4使用
  • 原文地址:https://www.cnblogs.com/25miao/p/7086563.html
Copyright © 2011-2022 走看看