zoukankan      html  css  js  c++  java
  • 泛型在CVS数据读取中的应用

    CVS是一个轻量级的数据库,文本之间是以 逗号分隔的。结合泛型,读取此种文件的方式如下:

    代码
    public List<Dictionary<string, string>> GetData(string filePath, out List<string> Column)
    {
    Column
    = new List<string>();
    List
    <Dictionary<string, string>> Data = new List<Dictionary<string, string>>();
    string strLine = "";
    string[] strArray;
    StreamReader sr
    = new StreamReader(filePath);
    strLine
    = sr.ReadLine();
    if (strLine == null || strLine == "")
    {
    return new List<Dictionary<string, string>>();
    }
    strArray
    = strLine.Split(',');
    foreach (string item in strArray)
    {
    Column.Add(item);
    }
    strLine
    = sr.ReadLine();
    while (strLine != null)
    {
    strArray
    = strLine.Split(',');
    Dictionary
    <string, string> dict = new Dictionary<string, string>();
    for (int i = 0; i < Column.Count; i++)
    {
    dict.Add(Column[i], strArray[i]);
    }
    Data.Add(dict);
    strLine
    = sr.ReadLine();
    }
    sr.Close();
    return Data;
    }

    调用方法如下:

      List<string> Column;
                List<Dictionary<string, string>> Date = GetData(this.textBox1.Text.Trim(), out Column);
                string str = "";
                foreach (Dictionary<string, string> item in Date)
                {
                    foreach (string item1 in Column)
                    {
                        str += item[item1] + "   ";
                    }
                    str += Environment.NewLine;
                       
                }
                this.textBox2.Text = str;

    怀揣着一点点梦想的年轻人
    相信技术和创新的力量
    喜欢快速反应的工作节奏
  • 相关阅读:
    数据结构 B/B+树
    Hadoop的目录结构
    安装JDK
    OSTEP-projects concurrency-webserver
    第二十四章(制作HTTP服务器端)学习笔记
    day4_生成小数的程序
    day4_用集合生成8位密码的程序
    day4_集合操作
    day3_homework
    day6_random模块的用法、break和continue
  • 原文地址:https://www.cnblogs.com/hfliyi/p/1903434.html
Copyright © 2011-2022 走看看