简体   繁体   English

如何加载多行文本文件?

[英]How can I load a textfile with many rows?

I have some textfiles which look similar to the following one: 我有一些文本文件看起来类似于以下文件:

0,75;1,20;true;4,50;4,30;3,30;false
0,75;1,20;true;4,50;4,30;4,30;true

I load the textfile like this: 我像这样加载文本文件:

private const string filename = "Data.txt";
string[] strs;
public void LoadTextfile()
    {
        System.IO.Stream stream = TitleContainer.OpenStream(filename);
        System.IO.StreamReader reader = new System.IO.StreamReader(stream);       
        strs = reader.ReadLine().Split(';');
        game1.StatsList.Add(new Containerclass.StatsContainer(float.Parse(strs[0], culture), float.Parse(strs[1], culture), bool.Parse(strs[2]), float.Parse(strs[3], culture), float.Parse(strs[4], culture), float.Parse(strs[5], culture), bool.Parse(strs[6])));        
        strs = reader.ReadLine().Split(';');
        game1.StatsList.Add(new Containerclass.StatsContainer(float.Parse(strs[0], culture), float.Parse(strs[1], culture), bool.Parse(strs[2]), float.Parse(strs[3], culture), float.Parse(strs[4], culture), float.Parse(strs[5], culture), bool.Parse(strs[6])));        

But my textfiles have much more entries(more than 100) than the textfile above. 但是我的文本文件比上面的文本文件具有更多的条目(超过100个)。 In addition, I don't know the number of rows of any textfile. 另外,我不知道任何文本文件的行数。 Maybe one textfile has 110 rows, the second textfile 150 rows, etc. Is it possible to load everything with a loop if I don't know the number of rows? 也许一个文本文件有110行,第二个文本文件有150行,依此类推。如果我不知道行数,是否可以通过循环加载所有内容? Which loop should I use to load the entire textfile? 我应该使用哪个循环来加载整个文本文件? I know that I can not use a for-loop because I don't know how much rows the textfile has, is it possible to load it with another loop? 我知道我不能使用for循环,因为我不知道文本文件有多少行,是否可以通过另一个循环加载它? Or is it not possible to load an entire textfile with one loop? 还是无法通过一个循环加载整个文本文件?

File.ReadLines(string path) is really nice File.ReadLines(string path)真的很好

string[] lines = File.ReadLines("filename.txt").ToArray();

If you really want to get fancy, you can try some linq 如果您真的想花哨的话,可以尝试一些linq

game1.StatsList = File.ReadLines("Data.txt")
    .Select(s => 
    {
        var strs = s.Split(';');
        return new Containerclass.StatsContainer(float.Parse(strs[0], culture), float.Parse(strs[1], culture), bool.Parse(strs[2]), float.Parse(strs[3], culture), float.Parse(strs[4], culture), float.Parse(strs[5], culture), bool.Parse(strs[6]));
    })
    .ToList();

Note that a for loop can be run without explicitly knowing the number of elements to iterate over - the central statement can be any conditional evaluating to a bool , not just i < number 请注意,可以在不显式知道要迭代的元素数量的情况下运行for循环-中央语句可以是对bool进行任何条件求bool ,而不仅仅是i < number

using (var reader = new StreamReader(stream))
{
    for(string line = reader.ReadLine();
        reader.EndOfFile == false;
        line = reader.ReadLine())
    {
        //...
    }
}

Using File.ReadLines will also allow you to iterate over each line using a foreach - 使用File.ReadLines还可以让您使用foreach遍历每行-

foreach(string line in File.ReadLines(filename))
{
    //...
}

Being a little more explicit, you can also use StreamReader.ReadToEnd and Splt the results 更明确一点,您也可以使用StreamReader.ReadToEndSplt结果

foreach(var line in new StreamReader(stream)
    .ReadToEnd()
    .Split(new [] {"\r\n" }, StringSplitOptions.RemoveEmptyEntries)
{
    //...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM