473,405 Members | 2,171 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,405 software developers and data experts.

FileStream

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
6 3197
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
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
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
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
--------------------
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Shawn | last post by:
Hi. I'm using a FileStream (instead of just the path to the xml file) to load an XmlDocument. I'm doing this because I need to be able to prevent other processes to update the file I'm working on....
3
by: Muki Rapp | last post by:
Hi! In the example below, once the media is full, the FileSteam.WriteByte throws an exception and the code is designed to handle it. However, when the GC is invoked, it calls the Finalize of...
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...
5
by: Chris Fink | last post by:
How do I load a string into a FileStream without going to disk? For example, string abc = "This is a string"; How do I load abc into a FileStream? FileStream input = new FileStream(.....);
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...
9
by: ljlevend | last post by:
I have two questions related to FileStreams. 1. Is there any way to determine whether a file has the permissions that are required by a FileStream constructor? For example, given the following...
7
by: Nathan Sokalski | last post by:
I am having a problem saving an image with the same name it originally had. I have two similar versions of my code, one in which I close the FileStream used to open the original image before saving,...
9
by: Tim_Mac | last post by:
hi, i'm not sure if i have chosen the best approach, but it seemed quite good to me. i have a collection class, containing business objects. the collection class is static and remains in-memory...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
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.