我想从textfile中读取特定行数 [英] I want to read specific number of lines from textfile

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

问题描述

HI,

i我正在将文本文件的内容读取到vb.the文本文件有大约100行,但我想只阅读前10行怎么做。



Dim TextBox1 As String:TextBox1 =D:\ temp.txt

Dim lines1()As String = System.IO.File.ReadAllLines(TextBox1)



i能够读取完整的txt文件内容,但我只需要10行。



我尝试过:



Dim TextBox1 As String:TextBox1 =D:\ temp.txt

Dim lines1()As String = System.IO.File.ReadAllLines(TextBox1)



i能够读取完整的txt文件内容,但我只需要10行。


i am reading the contents of textfile to vb.the textfile has around 100 lines,but i want to read only first 10 lines how to do it.

Dim TextBox1 As String : TextBox1 = "D:\temp.txt"
Dim lines1() As String = System.IO.File.ReadAllLines(TextBox1)

i am able to read full txt file contents,but i need only 10 lines of it.

What I have tried:

Dim TextBox1 As String : TextBox1 = "D:\temp.txt"
Dim lines1() As String = System.IO.File.ReadAllLines(TextBox1)

i am able to read full txt file contents,but i need only 10 lines of it.

推荐答案

迭代使用 StreamReader.ReadLine [ ^ ]方法。


c# - 如何读取文本文件中的指定行? - 堆栈溢出 [ ^ ]



示例代码

c# - How do I read a specified line in a text file? - Stack Overflow[^]

sample code
Dim line As String = File.ReadLines(FileName).Take(10).First()

Dim line As String = File.ReadLines(FileName).Skip(14).Take(1).First()


如果你想只阅读10行,请使用:

If you want to read 10 first lines only, please use this:
Dim lines = File.ReadLines(FileName).Take(10).ToList() 'returns List(Of String)


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

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