当前位置:编程学习 > C#/ASP.NET >>

VB.NET逐行读取文本

现有代码如下:
Dim rtxt As System.IO.StreamReader = New System.IO.StreamReader(My.Application.Info.DirectoryPath & "\data\list.0")
        Do While rtxt.EndOfStream = False
            Str1 = rtxt.ReadLine
            '这里有一些必要的操作,所以这个do while里不能写成ReadToEnd()
            '每一行都有一个操作,也就是说是按照行来处理的。
            '请问这里我如何让他movenext?
            '就是怎么让指针移到下一行?
        Loop
        rtxt.Close()
        rtxt.Dispose() --------------------编程问答-------------------- 对了,那个do whlie 写错了……
好像是应该写成 not .eof or .bof 什么的吧……
顺便帮忙改下,呵呵……
有点迷糊…… --------------------编程问答-------------------- Dim textArr As String() = File.ReadAllLines("a.txt") 
For Each s As String In textArr 
    Console.WriteLine(s) 
Next  --------------------编程问答-------------------- Dim textArr As String() = File.ReadAllLines("a.txt") 
这句中的file说什么未声明名称…… --------------------编程问答-------------------- Imports System.IO

引用 3 楼 siren7577 的回复:
Dim textArr As String() = File.ReadAllLines("a.txt")
这句中的file说什么未声明名称……
--------------------编程问答-------------------- Imports System.IO 
引用命名空间 --------------------编程问答-------------------- 我已经要睡觉了…明天起来测试 --------------------编程问答-------------------- Imports System.IO
说imports是语法错误,应为声明……
sorry啊,新手,请帮我搞明白。 --------------------编程问答-------------------- 还是没弄明白怎么到下一行……
这次一个字也没读出来…… --------------------编程问答-------------------- 学习 --------------------编程问答-------------------- 这样,你把File改成System.IO.File即可!
或者在项目中添加引用:system.IO --------------------编程问答--------------------
引用 10 楼 ggxuelei 的回复:
这样,你把File改成System.IO.File即可!
或者在项目中添加引用:system.IO


正确 --------------------编程问答--------------------
引用 2 楼 wuyq11 的回复:
Dim textArr As String() = File.ReadAllLines("a.txt")
For Each s As String In textArr
    Console.WriteLine(s)
Next



呵呵,就这啦! --------------------编程问答--------------------

Private Sub read_file()

        Try
            Dim fs As FileStream

            'declaring a FileStream to open the file named file.doc with access mode of reading
            fs = New FileStream(CONFIG_FILE, FileMode.Open, FileAccess.Read)

            'creating a new StreamReader and passing the filestream object fs as argument
            Dim reader As New StreamReader(fs)

            'Seek method is used to move the cursor to different positions in a file, in this code, to
            'the beginning
            reader.BaseStream.Seek(0, SeekOrigin.Begin)

            Dim line As String = "@

            While reader.Peek() > -1
                'peek method of StreamReader object tells how much more data is left in the file
                line = reader.ReadLine()
                Select Case line
                    Case "aaaaaaa"
                       doAAAAAA()
                    Case "bbbbbbb"
                        doBBBBB()
                    Case "symbol"
                        mode = MODE_SYMBOL
                    Case "row=0"
                        row = 0
                        col = 0
                    Case "row=1"
                        row = 1
                        col = 0
                    Case "row=2"
                        row = 2
                        col = 0
                    Case "row=3"
                        row = 3
                        col = 0
                    Case "row=4"
                        row = 4
                        col = 0

                    Case Else

                        'do nothing
                   
        
                End Select
            End While
            reader.Close()
        Catch ex As Exception
          
        End Try
    End Sub




我现在在做的程序就用这段代码。。。

--------------------编程问答-------------------- 上面的 Dim line As String = "@ 应该是 Dim line As String = ""
补充:.NET技术 ,  VB.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,