如何从文件中读取指定的行数 [英] How to read a specified number of lines in from a file

查看:72
本文介绍了如何从文件中读取指定的行数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!我正在尝试将文件读取为字符串,但是我只想读取一定数量的行.我该怎么做呢?

谢谢您的宝贵时间,

Suraci

解决方案

您要查看StreamReader.ReadLine().您应该能够自动使用此方法读取的行数.

(和我所说的你应该",这意味着,应该能够弄清楚它,因为它是可能的.我不只是猜测它会起作用).

您好Surcaci,

使用以下方法:

private string GetFirstLines(string filename, int linesCount)
{
  StringBuilder s = new StringBuilder();

  using (System.IO.StreamReader sr = new System.IO.StreamReader(filename))
  {
      for (int i = 0; i < linesCount; i++)
      {
         s.Append(sr.ReadLine());
      }
      
      sr.Close();
  }

  return s.ToString();
 }



希望对您有所帮助.


Hello all! I''m trying to read a file into a string, but I only want to read in a certain number of lines. How do I go about doing that?

Thank you for your time in advance,

Suraci

解决方案

you want to look at StreamReader.ReadLine(). You should be able to automate the number of lines read in using this.

(and by me saying "you should", it means, you should be able to figure it out because it''s possible. I''m not just guessing it will work).


Hi Surcaci,

Use the following method:

private string GetFirstLines(string filename, int linesCount)
{
  StringBuilder s = new StringBuilder();

  using (System.IO.StreamReader sr = new System.IO.StreamReader(filename))
  {
      for (int i = 0; i < linesCount; i++)
      {
         s.Append(sr.ReadLine());
      }
      
      sr.Close();
  }

  return s.ToString();
 }



I hope this helps.


这篇关于如何从文件中读取指定的行数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆