FileStream

R

rn5a

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

Dim objFile As New File(Server.MapPath("Page1.html"))
Dim sReader As StreamReader

'open the file
sReader = objFile.OpenText
--------------------

--------------------
'using the Open method

Dim objFile As New File(Server.MapPath("Page1.html"))
Dim objStream As Stream

objStream = objFile.Open(FileMode.OpenCreate, FileAccess.Read)
 
A

Aidy

The Stream object handles a stream of bytes. The FileStream object inherits
Stream and is geared toward files specifically. Using the FileStream you
can set access permissions on the file you are manipulating. StreamReader
inherits TextReader and is for streams of text (unlike the FileStream which
can handle any data such as binary images etc). The StreamReader allows you
to use specific encoding.
 
R

rn5a

Aidy, so that means whenever dealing with text files, StreamReader
should be used & when dealing with with binary files, FileStream should
be used, am I right? Also the FileMode, FileAccess & FileShare
parameters cannot be set using StreamReader; these 3 parameters can
only be set using FileStream.

Please correct me if I am wrong.
 
A

Aidy

Aidy, so that means whenever dealing with text files, StreamReader
should be used & when dealing with with binary files, FileStream should
be used, am I right? Also the FileMode, FileAccess & FileShare
parameters cannot be set using StreamReader; these 3 parameters can
only be set using FileStream.

That's pretty much it. Note that you can pass a Stream to StreamReader. So
if working with text files you can open the file with FileStream to utilise
FileAccess etc, and read the stream as text using StreamReader. If the file
is a binary one you can read it using just FileStream.

System.IO.FileStream fs = new System.IO.FileStream(@"c:\myfile.txt",
System.IO.FileMode.OpenOrCreate);

System.IO.StreamReader sr = new System.IO.StreamReader(fs);

string fileContent = sr.ReadToEnd();
 
R

rn5a

Aidy, please have a look at the following code:

--------------------
Dim objFile As New File(Server.MapPath("Page1.html"))
Dim sReader As StreamReader

sReader = objFile.OpenText
--------------------

In the above code, I am not creating an instance of the StreamReader
class but the code you have shown in your follow-up post creates an
instance of the StreamReader class. Both of them can be used to open a
file.

But why is it necessary to create an instance of the StreamReader class
in the example you have shown? How come the above code works without
creating an instance of the StreamReader class? What's the DIFFERENCE
between the above code & your code?

Thanks....
 
A

Aidy

--------------------
Dim objFile As New File(Server.MapPath("Page1.html"))
Dim sReader As StreamReader

sReader = objFile.OpenText

Your code isn't, but the OpenText method of the File class is creating one
for you and returning it. Essentially the OpenText is doing pretty much
what my code does only in a simpler, neater way.
class but the code you have shown in your follow-up post creates an
instance of the StreamReader class. Both of them can be used to open a
file.

But why is it necessary to create an instance of the StreamReader class
in the example you have shown?

Depends how you want to write your code. In .net there are often many ways
of doing the same thing, this is just one of those ways. The File class is
essentially a helper class that bundles useful file related functions in one
place. Like a code helper object.

Using FileStream, StreamReader etc as in my example is just a "purer" way of
coding .net. The two methods have the same result, just that my example was
a little more explicit in showing what it was doing.
How come the above code works without
creating an instance of the StreamReader class?

As I said above, the OpenText method is creating the class for you.
 
R

rn5a

Thanks a lot, Aidy. Your explanations & prompt responses have indeed
enlightened my knowledge on this topic. I got a lot of my doubts
clarified.

I'll get back to you in case I get stuck up somewhere else as far as
this topic is concerned. I hope you won't mind it.

Thanks once again,

Regards,

Ron
 

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

Members online

No members online now.

Forum statistics

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

Latest Threads

Top