【问题标题】:How to read multilines from text file in c#如何在c#中从文本文件中读取多行
【发布时间】:2014-10-27 10:43:05
【问题描述】:

我有一个包含用户记录的文本文件。在文本文件中,一个用户记录存在于三行文本文件中。现在根据我的要求,我必须为一个用户读取前三行,对其进行处理并插入数据库和第二个用户的下三行等等..

这是我用于从文本文件中单行读取的代码..

        if (System.IO.File.Exists(location) == true)
        {
            using (StreamReader reader = new StreamReader(location))
            {
                while ((line = reader.ReadLine()) != null)
                {     
                        line = line.Trim();
                }
        }   
        }

请帮我阅读多行,在本例中为文本文件中的 3 行..

谢谢..

【问题讨论】:

  • 使用循环计数器和模 3 条件一次加入 3 行

标签: c# .net text-files streamreader


【解决方案1】:

你可以这样做:

if (System.IO.File.Exists(location) == true)
        {
            var lines=File.ReadAllLines(location);
            int usersNumber = lines.Count() / 3;
            for(int i=0; i < usersNumber; i++){
                var firstField=lines[i*3];
                var secondField=lines[i*3 +1];
                var thirdField=lines[i*3 +2];
                DoStuffs(firstField,secondField,thirdField);
            }
            if(lines.Count() > usersNumber *3) //In case there'd be spare lines left
                 DoSomethingElseFrom(lines, index=(usersNumber*3 +1));
        }

您正在读取文件的所有行,计算您有多少用户(3 人组),然后为每个组检索其关联信息,最后您正在处理 3 人组与同一用户相关的字段。

【讨论】:

  • 能否提供投反对票的原因?这会更有建设性,因为我可以尝试改进答案。
  • 我没有对此投反对票,但首先认为这会起作用(原则上),但需要进行一些验证,以防行数不足。
  • 线条到底怎么会变短?进入循环前检查行数。
  • 好吧,简短的台词并不是我想说的。但要澄清一下,我的意思是如果文件中有 7 行,然后将其除以 3,则将为您提供 2 个用户。但是那里有一条额外的线,这就是我说需要更多验证的原因。你的代码应该运行。
  • 这就是为什么我说你的代码确实有效。我唯一可以错的是我给出的案例。只是想猜测为什么有人对此投反对票。
【解决方案2】:

我使用了一个包含以下内容的虚拟 dource 文件:

line1_1 /*First line*/
line1_2
line1_3
line2_1 /*second line*/
line2_2
line2_3
line3_1 /*third line*/
line3_2
line3_3
line4_1 /*fourth line*/
line4_2
line4_3

string result = String.Empty;
string location = @"c:\users\asdsad\desktop\lines.txt";
if (System.IO.File.Exists(location) == true)
    {
        using (StreamReader reader = new StreamReader(location))
        {
            string line = String.Empty;
            while ((line = reader.ReadLine()) != null) /*line has the first line in it*/
            {
                for(int i = 0; i<2; i++) /*only iterate to 2 because we need only the next 2 lines*/
                    line += reader.ReadLine(); /*use StringBuilder if you like*/
                result += line; 
            }
    }   
    result.Dump(); /*LinqPad Only*/

【讨论】:

  • 好的,知道了..但是在我的要求中,我必须将三行作为字符串而不是字符串列表集合作为您的解决方案中的字符串。如何实现这一点?
  • 你的意思是像 line1 + line2 + line3 ?
  • result.Dump(); 需要这个吗?
  • 您的内部 for 循环应该检查 reader.ReadLine() 是否成功,否则您最终可能会附加一个空值(在输入文件错误的情况下,例如,如果每个用户的行数很短,则为数字)。
  • @Hansal Mehta,没有转储方法只有在使用 LinqPad 时才有效。
【解决方案3】:
void Main()
{
    var location = @"D:\text.txt";
    if (System.IO.File.Exists(location) == true)
    {
        using (StreamReader reader = new StreamReader(location))
        {
            const int linesToRead = 3;
            while(!reader.EndOfStream)
            {
                string[] currReadLines = new string[linesToRead];
                for (var i = 0; i < linesToRead; i++)
                {
                    var currLine = reader.ReadLine();
                    if (currLine == null)
                        break;

                    currReadLines[i] = currLine;
                }

                //Do your work with the three lines here
                //Note; Partial records will be persisted
                //var userName = currReadLines[0] ... etc...
            }
        }
    }
}

【讨论】:

  • 你能告诉我如何在字符串而不是字符串数组中读取它。我只需要在字符串中吗?
猜你喜欢
  • 2011-10-24
  • 1970-01-01
  • 1970-01-01
  • 2011-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-03
相关资源
最近更新 更多