using vb.net read unix style text file

本文介绍了一种解决在.Net环境中使用StreamReader读取Unix格式文本文件遇到的问题的方法。通过创建一个继承自StreamReader的类,名为StreamReaderUnix,该类特别针对Unix格式的换行符进行处理,使得读取Unix文本文件变得更为顺畅。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

ExpandedBlockStart.gif代码

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

 

 

转载于:https://www.cnblogs.com/yangbin990/archive/2010/03/22/1691499.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值