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.IO
Public Class StreamReaderUnix
Inherits streamreader
Sub New(ByVal filename As String)
MyBase.New(filename)
End Sub
Public Function ReadLine(ByVal unixStyle As String) As String
Dim intByte As Integer
Dim bteRead() As Byte
Dim 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
Public Class StreamReaderUnix
Inherits streamreader
Sub New(ByVal filename As String)
MyBase.New(filename)
End Sub
Public Function ReadLine(ByVal unixStyle As String) As String
Dim intByte As Integer
Dim bteRead() As Byte
Dim 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
oRead = New StreamReaderUnix("sample.txt")
Dim lineIn As String
While oRead.Peek <> -1
lineIn = oRead.ReadLine("unix")
MsgBox(lineIn)
End While