FileStream & StreamReader?

A

Arpan

A file can be read using only the StreamReader object like this:

Dim sReader As StreamReader
sReader = New StreamReader(Server.MapPath("File1.txt"))
While(sReader.Peek > -1)
Response.Write(sReader.ReadLine)
End While

as well as using the FileStream object along with the StreamReader
object like this:

Dim fStream As FileStream
Dim sReader As StreamReader

fStream = New FileStream(Server.MapPath("File1.txt"), FileMode.Open,
FileAccess.Read)
sReader = New StreamReader(fStream)
While(sReader.Peek > -1)
Response.Write(sReader.ReadLine)
End While

Now how do I decide which way to go when I want to read a file?

If I am not mistaken, the second way would involve additional overheads
as compared to the first way, isn't it?

Thanks,

Arpan
 
K

KJ

I would only use the second way if I needed to open the stream in a
specific (non read-only) manner. Also, please remember to Dispose these
objects when you're done with them!
 
B

bruce barker \(sqlwork.com\)

they are the same. if you pass a path to Streamreader, it creates a
FileStream and wraps its.

-- bruce (sqlwork.com)
 
A

Arpan

they are the same. if you pass a path to Streamreader, it creates a
FileStream and wraps its

Does that mean there aren't any additional overheads involved when
using both the FileStream object & the StreamReader object as compared
to using just the StreamReader object to read files?

Arpan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

FileStream 6
StreamReader 1
How do I save information from an GUI into a XML-file? 0
StreamReader Problem 0
StreamReader Read? 1
System.Xml.XmlException 1
Exceptional Error 8
Stream Reader 2

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top