473,402 Members | 2,050 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

FileStream & StreamReader?

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

Aug 30 '06 #1
3 3087
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!

Arpan wrote:
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
Aug 30 '06 #2
they are the same. if you pass a path to Streamreader, it creates a
FileStream and wraps its.

-- bruce (sqlwork.com)
"Arpan" <ar******@hotmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
>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

Aug 30 '06 #3
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
bruce barker (sqlwork.com) wrote:
they are the same. if you pass a path to Streamreader, it creates a
FileStream and wraps its.

-- bruce (sqlwork.com)
"Arpan" <ar******@hotmail.comwrote in message
news:11*********************@i42g2000cwa.googlegro ups.com...
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
Aug 31 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tom | last post by:
I am working with the this object as oppose to the StreamReader object becuase I need to access a file (to find the contents) while an external application is updating the file. When I was...
4
by: John Salerno | last post by:
In Murach's C#, it shows that StreamWriter and StreamReader both take a stream object as a parameter, and the book's examples of these two classes all first create a FileStream object. But I notice...
3
by: sbparsons | last post by:
I have a file opened as a FileStream. I have a StreamReader and StreamWriter object opened, referencing the FileStream object. My aim is to read lines from the file until I find the line where...
0
by: lh | last post by:
The following method only works when i give the ASP.net account full permissions on the directory. It doesn't work when i give the directory Modify, Read &Execute, List Folder Contents, Read, and...
1
by: iwdu15 | last post by:
hi...just a quick question. what are the differences in using a FileStream and StreamWriter opposed to just a StreamWriter.....for instance Dim fs as New FileStream("C:\Test.txt",...) Dim sw As...
2
by: cj | last post by:
VB2005 I've opened files and read them using r = new io.streamreader("c:\thisfile.txt") line = r.readline Now I see an example where they are doing dim objopenfile as io.filestream = new...
6
by: rn5a | last post by:
What's the difference between the 'Stream' object & the 'FileStream' object? A file can be opened using the following code snippets: -------------------- 'create a File object & StreamReader...
11
by: honguin | last post by:
Hi, With the following code, I have created a web request to a url which I am making a HTML POST with the html page request.htm, even though it makes a HTML POST, the StreamReader produces a XML...
4
by: tshad | last post by:
If I have the following: fs = new FileStream(xmlFile, FileMode.Open, System.IO.FileAccess.Read); sr = new StreamReader(fs); will sr.Close() also close the fs? Or do I need to close both? ...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.