
























Unix text file uses linefeed only to indicate end of line, the .net streamreader's readline function is not working with unix text file.
I made a small wrapper for StreamReader class to make it is working with unix format.
At first, I created a class
代码
Imports System.IOPublic Class StreamReaderUnix
Inherits streamreader
Sub New(ByVal filename As String)
MyBase.New(filename)
End SubPublic Function ReadLine(ByVal unixStyle As String) As String
Dim intByte As Integer
Dim bteRead() As ByteDim mybuffer(1) As Char
Dim lineFeedLocation As Integer
Dim aLine As String = String.Empty If unixStyle = "" Then
MyBase.ReadLine()
Else
Do While Not intByte = -1
intByte = MyBase.Read(mybuffer, 0, 1)
If intByte <> -1 Then
lineFeedLocation = Array.IndexOf(mybuffer, CChar(vbLf))
If mybuffer(0) = CChar(vbLf) Then
Return aLine
ElseIf mybuffer(0) = CChar(vbCr) Then
'doing nothing
Else
aLine = aLine & mybuffer(0)
End If
End If
Loop
End If
End Function
End Class
Below is the sample code to use this class
Dim oRead As StreamReaderUnix
oRead = New StreamReaderUnix("sample.txt")
Dim lineIn As String
While oRead.Peek <> -1
lineIn = oRead.ReadLine("unix")
MsgBox(lineIn)
End While
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。