Programming 笔记

工作中遇到的问题就记载这里

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

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 StringAs String
        
Dim intByte As Integer
        
Dim bteRead() As Byte

        
Dim mybuffer(1As 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, 01)
                
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

 

 

posted on 2010-03-22 12:30  IT 笔记  阅读(381)  评论(0编辑  收藏  举报