从文本文件中读取文本 [英] Read Text From Text File

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

问题描述



我需要一个代码来读取d.txt第二行的所有文字。



帮助我。

Hi,
I need a code to read all text from second line of "d.txt".

Help Me.

推荐答案

有很多方法可以做到这一点,但由于这有很多关于它的功课,我不会给你任何代码!< br $> b $ b

第一种方法是使用 File.ReadAllLines [ ^ ]然后选择它返回的字符串数组的第二行,如果有的话。



另一种是创建一个StreamReader,然后使用<循环读取每一行a href =http://msdn.microsoft.com/en-us/library/system.io.streamreader.readline.aspx> StreamReader.ReadLine.aspx [ ^ ]直到你到达线号你想要的。



第一个是快速的,无论是代码还是执行,但是对于大文件使用大量内存。第二种是编码和执行速度较慢,但​​占用的内存空间很小。您的选择!
There are a number of ways you could do this, but since this has a heavy smell of homework about it, I won't give you any code!

The first way is to use File.ReadAllLines[^] and then select the second line of the string array it returns, if there is one.

The other is to create a StreamReader, then read each line sequentially in a loop using StreamReader.ReadLine.aspx[^] until you get to the line number you want.

The first is quick, both to code and to execute, but uses a huge amount of memory for large files. The second is slower to code and to execute, but uses a small fraction of the memory space. Your choice!


Google是您的朋友点击我!



以上是一个简单的谷歌搜索链接,其短语是:



读取第二行文本文件c#



如果查看结果,您会看到第5个链接实际上直接链接了Code Project文章。



我建议您尝试阅读其中一些链接。
Google is your friend so click me!

So above is a link to a simple google search, the phrase of which is:

read second line of text file c#

If you look at the results you will see that 5th link actually links directly a Code Project article.

I suggest you try reading some of those links.


try
      {
          using (StreamReader sr = new StreamReader("d.txt"))
          {
              String line = sr.ReadToEnd();
              Console.WriteLine(line);
          }
      }
      catch (Exception e)
      {
          Console.WriteLine("The file could not be read:");
          Console.WriteLine(e.Message);
      }


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

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