By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,932 Members | 1,462 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,932 IT Pros & Developers. It's quick & easy.

FileStream

P: n/a
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)
--------------------

--------------------
'using the FileStream object

Dim fStream As New FileStream(Server.MapPath("Page1.html"),
FileMode.OpenOrCreate)
--------------------

Now what's the difference between the above 3 code snippets? How do I
understand under what circumstances should one method be used instead
of the other two?

Jan 10 '07 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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.

<rn**@rediffmail.comwrote in message
news:11**********************@i56g2000hsf.googlegr oups.com...
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)
--------------------

--------------------
'using the FileStream object

Dim fStream As New FileStream(Server.MapPath("Page1.html"),
FileMode.OpenOrCreate)
--------------------

Now what's the difference between the above 3 code snippets? How do I
understand under what circumstances should one method be used instead
of the other two?

Jan 10 '07 #2

P: n/a
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.
Aidy wrote:
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.

<rn**@rediffmail.comwrote in message
news:11**********************@i56g2000hsf.googlegr oups.com...
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)
--------------------

--------------------
'using the FileStream object

Dim fStream As New FileStream(Server.MapPath("Page1.html"),
FileMode.OpenOrCreate)
--------------------

Now what's the difference between the above 3 code snippets? How do I
understand under what circumstances should one method be used instead
of the other two?
Jan 10 '07 #3

P: n/a
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();
Jan 10 '07 #4

P: n/a
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....

Aidy wrote:
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();
Jan 10 '07 #5

P: n/a
--------------------
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
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.
Jan 10 '07 #6

P: n/a
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
Aidy wrote:
--------------------
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

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.
Jan 10 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.