Using String.Split and FileStream-.Net 1 and 2





5
Date Submitted Tue. Sep. 5th, 2006 8:05 PM
Revision 1 of 1
Beginner MovingParts
Tags FileStream | StreamReader | String | String.Split | VB
Comments 0 comments
One of best and unknown built in string function included in .NET is String.Split(). It has come in quite handy for my in the last 2 years that I thought I'd share. Pretty basic example here...and for my first post, I thought I'd throw in a little File IO (for free of course)! This was written in .NET 2.0 and it will take little to no modification to make it work in 1.0/1.1.

Private Function Foo() As String()
        'THE PURPOSE OF THIS FUNCTION IS TO READ A TAB DELIMITED FILE
        'AND RETURN ITS CONTENTS IN A STRING ARRAY

        '-----------------------------------------------------------------------
        'INPUT FROM FILE COMMA DELIMITED FILE (C:\TEMP\SAMPLE.CSV)
        '-----------------------------------------------------------------------
        '[START OF FILE]
        'JOHNNY, CACHE, 1024 BYTE STREET, SAN ANTONIO, TEXAS, 75852
        '[END OF FILE]
        '-----------------------------------------------------------------------


        '-----------------------------------------------------------------------
        'OUTPUT INTO ARRAY (arr)
        '-----------------------------------------------------------------------
        'arr(0) = JOHNNY
        'arr(1) = CACHE
        'arr(2) = 1024 BYTE STREET
        'arr(3) = SAN ANTONIO
        'arr(4) = TEXAS
        'arr(5) = 75852
        '-----------------------------------------------------------------------


        '-----------------------------------------------------------------------
        'DECLARE VARIABLES
        '-----------------------------------------------------------------------
        Dim strFilePath As String = "c:\temp\sample.csv"
        Dim fileStream As IO.FileStream
        Dim streamReader As IO.StreamReader
        Dim s As String = ""
        Dim arr(-1) As String

        'DECIDE WHAT CHARACTER (OR EVEN A STRING) WILL BE USED TO SPLIT THE STRING INTO AN ARRAY
        Dim c As Char = vbTab 'tab delimited
        'Dim c As Char = ","c 'implicit cast from String to Char
        '-----------------------------------------------------------------------

        If IO.File.Exists(strFilePath) Then
            Try
                'OPEN THE PHYSICAL STREAM
                fileStream = New IO.FileStream(strFilePath, IO.FileMode.Open)

                'OPEN THE LOGICAL STREAM
                streamReader = New IO.StreamReader(fileStream)

                'READ THE STREAM INTO A STRING
                s = streamReader.ReadToEnd

                'USE String.Split() TO AUTOMATICALLY DIMENSION AND POPLULATE THE ARRAY!
                arr = s.Split(c)

            Catch ex As Exception
                Throw ex
            Finally

                '-----------------------------------------------------------------------
                'REALEASE ANY RESOURCES THAT MAY BE TIED UP IN THE STREAMS
                '-----------------------------------------------------------------------
                If Not fileStream Is Nothing Then
                    fileStream.Close()
                End If

                If Not streamReader Is Nothing Then
                    streamReader.Close()
                End If
                '-----------------------------------------------------------------------

            End Try

        Else
            '-----------------------------------------------------------------------
            'INFORM USER THAT THEY ARE IN ERROR
            '-----------------------------------------------------------------------
            Throw New Exception("File " & strFilePath & " was not found, n00b.")
            '-----------------------------------------------------------------------
        End If

        Return arr
    End Function
 

Daniel Kunkel

Live life regretting the things you did and not the things you never attempted.

Comments

There are currently no comments for this snippet.

Voting