Streamreader questions

Xombie

Member
Joined
May 1, 2003
Messages
6
Got a couple of questions on streamreader/filestream. For reference, Im using vb.net ent arch 2003.

1. Im on a US based windows 2000 install. I have a text file that I want to read. The text file could contain asian characters but should contain primarily ascii characters. I think Im saying that right. Anyway, can I use....
Code:
Dim sr As StreamReader = New StreamReader(New FileStream(inFile, FileMode.Open), Encoding.Default, False, 4096)

... to read the file and still keep the asian characters intact? The part thats reading it is...
Code:
        Do While sr.ReadBlock(cBuffer, 0, cBuffer.Length - 1) > 0
            bHold = Encoding.Default.GetBytes(cBuffer)
            sBuild.Append(Encoding.Default.GetString(bHold))
        Loop

... where sBuild is a stringbuilder and bHold is a byte array. And yes, I did steal this stuff from the vbextreme example.

2. Is there a speedy way to break up the text into lines? From vbextreme the fella said ReadLine was slow so Im using ReadBlock but then I still need to split the read text into lines and then parse it for certain text. Should I use ReadLine in this case or go for Splitting the text and then looping through it?

Update:

I decided to change over to ReadLine since the routine Im using is a custom INI file reader and I could shortcircuit it when I hit the value. Testing the VB.Net code against the original VB6 could of mine, it seems the VB.Net code is a little over twice as slow. Return a value in vb6 takes about 0.00105153 seconds while VB.Net return the same value in 0.002876901 seconds. The code is the same except Im using...

Code:
        Dim sr As StreamReader = New StreamReader(New FileStream(inFile, FileMode.Open, FileAccess.Read, FileShare.Read), Encoding.Default)

.... in VB.Net (along with ReadLine) rather than the VB6 Open For Input As/Line Input method.

Is this normal or should I be doing something else in vb.net?
 
Last edited by a moderator:
try using a TextReader instead of a StreamReader (TextReader is a StreamReader, but of the text variety)
 
Actually, I got the reading part to work just fine. Its even faster than vb6 now. I did away with VBCompatability and made a few other changes and its okay now. Oh, and I simply used ReadLine.

Now my other question deals with writing a value. The chokepoint on that function is when I open & close a file to write. A simple sw = File.CreateText(inFile) call slows the function down a lot. And closing the file does the a good bit of damage as well.

Any pointers on writing a new file with StreamWritter without using file.createtext? Something faster maybe?
 
straight from msdn:
Code:
Imports System
Imports System.IO

Class Test
    Public Shared Sub Main()
         Create an instance of StreamWriter to write text to a file.
        Dim sw As StreamWriter = New StreamWriter("TestFile.txt")
         Add some text to the file.
        sw.Write("This is the ")
        sw.WriteLine("header for the file.")
        sw.WriteLine("-------------------")
         Arbitrary objects can also be written to the file.
        sw.Write("The date is: ")
        sw.WriteLine(DateTime.Now)
        sw.Close()
    End Sub
End Class
hope this helps:)
 
Back
Top