写入多个开放的文本流

时间:2012-04-11 23:12:47

标签: vb.net filesystemobject

我是vb.net的新手。我有很多使用vb的经验。我想知道的是如何替换文件系统对象。具体来说,我想使用文本流数组。我将如何在vb.net中编写以下内容

为了简单起见,我假设我有一个长度为1个字节的文本文件,它只是一个从0到9的重复#系列。我希望任何有1的记录转到tsout (1),2到tsout(2)等...

dim fso as new filesystemobject, tsIN as textstream, tsOut(10) as textstream
dim lineIN, i
set tsin = fso.opentextfile("C:\input.txt",forreading,false)
for i = 1 to 10
 set tsout(i) = fso.createtextfile("C:\output" & i & ".txt",true)
next
do while not tsin.atendofstream
 linein = mid(tsin.readline,1,1)
 tsout(linein).writeline(linein)
loop
for i = 1 to 10
 tsout(i).close
next
tsin.close

1 个答案:

答案 0 :(得分:0)

使用System.IO.StreamWriterSystem.IO.StreamReader s:

Using in As New StreamReader("C:\input.txt")
    Dim out(9) As StreamWriter

    For i As Integer = 0 To 9
        out(i) = New StreamWriter("C:\output" & i & ".txt")
    Next

    While in.Position < in.Length
        Dim l As Integer = Integer.Parse(in.ReadLine().Substring(1, 1))

        out(l).WriteLine(l.ToString())
    End while

    For i As Integer = 0 To 9
        out(i).Close()
        out(i).Dispose()
    Next
End Using

注意:这假设{/ 1}}位于文件的开头或项目范围内。