流式传输文本文件

时间:2017-07-21 14:04:54

标签: c#

我有一段代码应该流式传输这个文本文件:

1.1.1.1.1.1.1.1.1.1.1.1.1.1.1
1.1.1.2.2.2.2.1.1.1.2.2.1.1.1
1.2.2.2.2.2.2.2.2.2.2.2.2.1.1
1.1.2.2.2.2.2.2.2.2.2.2.2.1.1
1.1.2.2.2.2.2.2.2.2.2.2.1.1.1
1.1.1.1.2.2.2.2.2.2.2.2.1.1.1
1.1.1.1.2.2.2.2.2.2.1.1.1.1.1
1.1.1.1.1.1.2.2.2.1.1.1.1.1.1
1.1.1.1.1.1.1.2.1.1.1.1.1.1.1
1.1.1.1.1.1.1.1.1.1.1.1.1.1.1
1.1.1.1.1.1.1.1.1.1.2.2.1.1.1
1.1.1.1.1.1.1.1.1.1.1.1.1.1.1

一切都很好,有点儿。我的目的是逐个翻找所有角色,然后在新线上完成。这就是我的小问题出现的地方,我一直试图整夜修复。

它很好地读取了第一行,但是它没有读取第二行......

以下是代码:

System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Text\TextFile.txt"); 

int loadX = 0;
int loadY = 0;
string line;

while (true)
{
    if (loadX <= 12)
    {
        loadX++;
        while ((line = file.ReadLine()) != null)
        {
            System.Threading.Thread.Sleep(500);
            string[] entries = line.Split('.');
            System.Console.Write(entries[loadX]);
            loadY++;
        }
    }

    System.Threading.Thread.Sleep(500);
    Console.Write($" Finished {loadX}");
    loadX = 0;
}

4 个答案:

答案 0 :(得分:0)

当你有更简单,更易读的处理方式时,使用while循环和StremReader似乎很愚蠢。

您可以通过File.ReadAllLines()

轻松简化代码
//lines will be a string array
var lines = File.ReadAllLines(@"C:\Text\TextFile.txt");
for(int x = 0; x < lines .Length; x++)
{
    var cols = lines [x].Split('.');
    for(int y = 0; y < cols.Length; x++)
    {
        //Here you have access to the value, and the x and y position
        Console.WriteLine("x: {0}, y: {1} value: {2}", x, y, cols[y]);
    }   
}

演示here

答案 1 :(得分:0)

var lines = File.ReadAllLines(@"C:\Text\TextFile.txt");
var points = lines.SelectMany((l, x) => l.Split('.').Select((s, y) => new {X = x, Y = y, Value = s}));
foreach (var point in points)
{
    Console.WriteLine($"({point.X}, {point.Y})={point.Value}");
}

答案 2 :(得分:-1)

如果你想用X和Y来浏览这个文件中的每个数字,那么这是一个例子,你可以这样做:

string[] lines = File.ReadAllLines(@"C:\Text\TextFile.txt");

for(int indexY = 0; indexY < lines.Length; indexY++){
    string[] lineEntries = lines[indexY].Split('.');

    for(int indexX = 0; indexX < lineEntries; indexX++){
        // here you have one number by accessing
        // it with lineEntries[indexX]
        Console.Write(lineEntries[indexX]);
    }
    Console.WriteLine();
}

答案 3 :(得分:-2)

你错误地认为它正在阅读第一行。实际上,您当前的代码读取每行的第一个值。由于您的输入,这恰好与第一行的输出相似,这会导致您的混淆。

您的主循环应循环遍历每一行,然后您可以处理该行并循环遍历每个值。然后你可以随意使用它。

以下是一个例子:

using(System.IO.StreamReader file = new System.IO.StreamReader(@"C:\Text\TextFile.txt"))
{
    int loadX = 0;
    int loadY = 0;
    string line;

    // Loop through each line as you read it.
    while ((line = file.ReadLine()) != null)
    {
        // Split the line to get an array of values.
        string[] entries = line.Split('.');

        // Loop through each value and process.
        for(int i = 0; i < entries.length; i++)
        {
            string entry = entries[i];

            // TODO: Do something with entry.

            loadY++;
        }

        loadX++;
    }
}

显然,在此示例中,loadXloadY未被使用,但这演示了如何正确递增它们,以便您可以根据需要使用它们。

提示:使用SteamReader时,您应该确保正确处理它,最好将其包含在using块中。