从文件中读取数据和存储的最佳方法?

时间:2015-04-23 19:03:10

标签: c# filereader streamreader

我有一个以这种方式存储数据的文本文件:

Player name: NAME HERE
Speed: 7
Strength: 9
Stamina: 4
Player name: ANOTHER NAME HERE
Speed: 5
Strength: 8
Stamina: 3

同一个文件包含大约十五个不同的玩家,我想从每个玩家收集值,存储在临时竞赛变量中,然后计算速度力量和耐力的平均值并呈现胜利者。我的第一个想法是使用正则表达式,但我目前正在调查是否有另一种更可持续的方法来执行此操作。

这是我的一个侧面项目,欢迎任何帮助。感谢

3 个答案:

答案 0 :(得分:1)

定义种族类结构:

public class Race
{
    public string Name;
    public int Speed;
    public int Strength;
    public int Stamina;
}

从您的文本文件中读取您的数据,并实例化您的Race个对象并将其收集到List<Race>。收集后,您可以从Average()致电List以获取结果。

string[] myTextFileLines = File.ReadAllLines(myTextFile);

List<Race> myRaces = new List<Race>();
for (int i = 0; i < myTextFileLines.Length; i += 4)
{
    myRaces.Add(new Race()
                    {
                        Name = myTextFileLines[i].Substring(myTextFileLines[i].IndexOf(":") + 2),
                        Speed = Convert.ToInt32(myTextFileLines[i + 1].Substring(myTextFileLines[i + 1].IndexOf(":") + 2)),
                        Strength = Convert.ToInt32(myTextFileLines[i + 2].Substring(myTextFileLines[i + 2].IndexOf(":") + 2)),
                        Stamina = Convert.ToInt32(myTextFileLines[i + 3].Substring(myTextFileLines[i + 3].IndexOf(":") + 2)),
                    });
}

Console.WriteLine("Avg Speed: {0}", myRaces.Average(r => Convert.ToDouble(r.Speed)));
Console.WriteLine("Avg Strength: {0}", myRaces.Average(r => Convert.ToDouble(r.Strength)));
Console.WriteLine("Avg Stamina: {0}", myRaces.Average(r => Convert.ToDouble(r.Strength)));
Console.ReadLine();

结果(使用您在问题中提供的确切数据):

enter image description here

答案 1 :(得分:1)

XML解决方案的一个示例:

Players.xml

var labels = new Dictionary<string, string>() 
{
    { "en_US", "English Label" },
    { "fr_CA", "French Label" }
};

var request = new RestRequest("/xxx/yyy", Method.POST) { RequestFormat = DataFormat.Json };
request.AddHeader("apikey", myApiKey);
request.AddParameter("user_key", myUserKey);
request.AddParameter("client_id", myClientId);
request.AddParameter("label", ???);

var client = new RestSharp.RestClient("https://api.3rdparty.com")
{
    Timeout = timeout,
    UserAgent = "My .NET REST Client"
};
var response = client.Execute(request);

C#代码

<?xml version="1.0" encoding="utf-8" ?>
<players>
  <player name="Alice">
    <speed>7</speed>
    <strength>9</strength>
    <stamina>4</stamina>
  </player>
  <player name="Bob">
    <speed>5</speed>
    <strength>8</strength>
    <stamina>3</stamina>
  </player>
</players>

输出

    private class Player
    {
        public string Name { get; set; }
        public int Speed { get; set; }
        public int Strength { get; set; }
        public int Stamina { get; set; }

        public override string ToString()
        {
            return "Name: " + Name + Environment.NewLine +
                   "Speed: " + Speed + Environment.NewLine +
                   "Strength: " + Strength + Environment.NewLine +
                   "Stamina: " + Stamina + Environment.NewLine;
        }
    }

    private static void PrintXmlPlayers(XmlNode players)
    {
        foreach (XmlNode player in players.SelectNodes("player"))
        {
            string playerName = player.Attributes["name"].InnerText;
            int playerSpeed = XmlConvert.ToInt32(player["speed"].InnerText);
            int playerStrength = XmlConvert.ToInt32(player["strength"].InnerText);
            int playerStamina = XmlConvert.ToInt32(player["stamina"].InnerText);

            Player aPlayer = new Player
            {
                Name = playerName,
                Speed = playerSpeed,
                Strength = playerStrength,
                Stamina = playerStamina
            };

            Console.WriteLine(aPlayer);
        }
    }

    private static void TestXml()
    {
        // path of document is relative to executable
        const string xmlPath = "../../Players.xml";

        XmlDocument doc = new XmlDocument();
        doc.Load(xmlPath);

        XmlNode players = doc["players"];
        PrintXmlPlayers(players);

        XmlNode alice = players.SelectSingleNode("player[@name='Alice']");
        string aliceOldString = alice["strength"].InnerText;
        alice["strength"].InnerText = "10";
        doc.Save(xmlPath);

        PrintXmlPlayers(players);

        alice["strength"].InnerText = aliceOldString;
        doc.Save(xmlPath);
    }

答案 2 :(得分:0)

如果文件格式是固定的,一种方法是使用System.IO.File.ReadAllLines获取文件中的每一行,然后循环遍历行,拆分键(:的左侧) )和值(:的右侧)。使用您在处理每一行时从文件中读取的值填充某种数据对象(下例中为PlayerObject)。

PlayerObject player = new PlayerObject();
string[] allLines = System.IO.File.ReadAllLines(FILE_PATH_HERE);
foreach(string dataPair in allLines)
{
    string[] pair = dataPair.Split(new[] { ':' });
    if(pair.Length == 2)
    {
        switch(pair[0])
        {
            case "Player name": /*process pair[1]*/ break;
            case "Speed": /*process pair[1]*/ break;
            //other possible key strings here
            //recommend defining these as constants
        }
    }
}

这将允许您重新排序文件中的参数(或至少支持不同的顺序),如果缺少任何参数,可以将它们预先设置为数据对象中的某个默认值。

您的格式中唯一缺少的是记录之间的某种哨兵值。现在你可以假设每个玩家都被一个新的“玩家名称”条目描绘出来,并在你读下一个“玩家名称”后开始填充下一个PlayerObject