Fundamental File Streaming |
|
Introduction to File Streaming |
|
|
File streaming consists of performing one of the routine
operations on a file, such as creating it or opening it. This basic operation
can be performed using a class called FileStream.
You can use a FileStream object to get a stream ready for
processing.
|
As one of the most complete classes of file processing of the .NET
Framework, FileStream is equipped with all necessary properties and
methods. To use it, you must first declare a variable of it. The class is
equipped with nine constructors.
One of the constructors (the second) of the FileStream
class has the following syntax:
Public Sub New(ByVal path As String, ByVal mode As FileMode)
This constructor takes as its first argument the name or the
file or its path. The second argument specifies the type of operation to perform
on the file. Here is an example:
Imports System
Imports System.IO
Public Class Exercise
Public Shared Sub main()
Dim NameOfFile As String = "Persons.spr"
Dim fstPersons As FileStream = New FileStream(NameOfFile, FileMode.Create)
End Sub
End Class
A streaming operation is typically used to create a stream.
Once the stream is ready, you can write data to it. The writing operation is
perform through various classes. One of these classes is BinaryWriter.
The BinaryWriter class can be used to write values of
primitive data types (char, int, float, double,
etc). To use a BinaryWriter value, you can first declare its variable. To do
this, you would use one of the class' three constructors. One of its
constructors (the second) has the following syntax:
Public Sub New(ByVal output As Stream)
This constructor takes as argument a Stream value,
which could be a FileStream variable. Here is an example:
Imports System
Imports System.IO
Public Class Exercise
Public Shared Sub main()
Dim NameOfFile As String = "Persons.spr"
Dim fstPersons As FileStream = New FileStream(NameOfFile, FileMode.Create)
Dim wrtPersons As BinaryWriter = New BinaryWriter(fstPersons)
End Sub
End Class
Most classes that are used to add values to a stream are
equipped with a method called Write. This is also the case for the BinaryWriter
class. This method takes as argument the value that must be written to the
stream. The method is overloaded so that there is a version for each primitive
data type. Here is an example that adds strings to a newly created file:
Imports System
Imports System.IO
Public Class Exercise
Public Shared Sub main()
Dim NameOfFile As String = "Persons.spr"
Dim fstPersons As FileStream = New FileStream(NameOfFile, FileMode.Create)
Dim wrtPersons As BinaryWriter = New BinaryWriter(fstPersons)
wrtPersons.Write("James Bloch")
wrtPersons.Write("Catherina Wallace")
wrtPersons.Write("Bruce Lamont")
wrtPersons.Write("Douglas Truth")
End Sub
End Class
When you use a stream, it requests resources from the
operating system and uses them while the stream is available. When you are not
using the stream anymore, you should free the resources and make them available
again to the operating system so that other services can use them. This is done
by closing the stream.
To close a stream, you can can call the Close() method of
the class(es) you were using. Here are examples:
Imports System
Imports System.IO
Public Class Exercise
Public Shared Sub main()
Dim NameOfFile As String = "Persons.spr"
Dim fstPersons As FileStream = New FileStream(NameOfFile, FileMode.Create)
Dim wrtPersons As BinaryWriter = New BinaryWriter(fstPersons)
wrtPersons.Write("James Bloch")
wrtPersons.Write("Catherina Wallace")
wrtPersons.Write("Bruce Lamont")
wrtPersons.Write("Douglas Truth")
wrtPersons.Close()
fstPersons.Close()
End Sub
End Class
Practical
Learning: Writing to a Stream |
|
- To be able to complete a file, change the Save() method in the
IceCream.vb file as follows:
Public Sub Save()
Dim NameOfFile As String
Console.WriteLine("Please enter your initials or the name " & _
"we will use to remember your order: ")
NameOfFile = Console.ReadLine()
NameOfFile = NameOfFile & ".icr"
' Find out if the user entered a name of a file that " & _
"is already in the machine
If File.Exists(NameOfFile) Then
Dim Answer As String
Dim stmIceCream As FileStream = _
New FileStream(NameOfFile, FileMode.Create)
Dim bnwIceCream As BinaryWriter = New BinaryWriter(stmIceCream)
' If so, find out if the user wants to replace the old file
Console.WriteLine("The file you entered exists already.")
Console.Write("Do you want to replace it(y/n)?")
Answer = Console.ReadLine()
' If the customer wants to replace it...
If Answer = "y" Or Answer = "Y" Then
' ... do so
Console.WriteLine("The former order with the " & _
"same name will be replaced")
Console.WriteLine(vbCrLf & "=-= Ice Cream Vending Machine =-=")
Console.WriteLine(" Saving Order: {0}", NameOfFile)
bnwIceCream.Write(Flavors(ChoiceFlavor))
bnwIceCream.Write(Containers(ChoiceContainer))
bnwIceCream.Write(Ingredients(ChoiceIngredient))
bnwIceCream.Write(Scoops)
bnwIceCream.Write(TotalPrice)
' If the customer wants to save the new order with a different name
ElseIf Answer = "n" Or Answer = "N" Then
' Ask the user to enter a name to remember the order
Console.Write("Please enter a name we will " & _
"use to remember this order: ")
NameOfFile = Console.ReadLine()
NameOfFile = NameOfFile & ".icr"
stmIceCream = New FileStream(NameOfFile, FileMode.Create)
bnwIceCream = New BinaryWriter(stmIceCream)
Console.WriteLine(vbCrLf & "=-= Ice Cream Vending Machine =-=")
Console.WriteLine(" Saving Order: {0}", NameOfFile)
bnwIceCream.Write(Flavors(ChoiceFlavor))
bnwIceCream.Write(Containers(ChoiceContainer))
bnwIceCream.Write(Ingredients(ChoiceIngredient))
bnwIceCream.Write(Scoops)
bnwIceCream.Write(TotalPrice)
Else
Console.WriteLine("Invalid Answer - We will close")
bnwIceCream.Close()
stmIceCream.Close()
End If
Else
Dim stmIceCream As FileStream = _
New FileStream(NameOfFile, FileMode.Create)
Dim bnwIceCream As BinaryWriter = New BinaryWriter(stmIceCream)
Console.WriteLine(vbCrLf & "=-= Ice Cream Vending Machine =-=")
Console.WriteLine(" Saving Order: {0}", NameOfFile)
bnwIceCream.Write(Flavors(ChoiceFlavor))
bnwIceCream.Write(Containers(ChoiceContainer))
bnwIceCream.Write(Ingredients(ChoiceIngredient))
bnwIceCream.Write(Scoops)
bnwIceCream.Write(TotalPrice)
bnwIceCream.Close()
stmIceCream.Close()
End If
End Sub
- Save the file and switch to the Command Prompt
- To compile it, type vbc /out:"Ice Cream Vending Machine".exe
IceCream.vb Exercise.vb and press Enter
- To execute it, type "Ice Cream Vending Machine" and
press Enter. Here is an example:
Have you ordered here before(y/n)? n
=-= Ice Cream Vending Machine =-=
------ New Customer Order ------
What type of flavor do you want?
1 - Vanilla
2 - Cream of Cocoa
3 - Chocolate Chip
4 - Organic Strawberry
5 - Butter Pecan
6 - Cherry Coke
7 - Chocolate Brownies
8 - Caramel Au Lait
9 - Chunky Butter
10 - Chocolate Cookie
Your Choice? 7
What type of container do you want?
1 - Cone
2 - Cup
3 - Bowl
Your Choice? 1
Do you want an ingredient or not
1 - No Ingredient
2 - Peanuts
3 - M & M
4 - Cookies
Your Choice? 4
How many scoops(1, 2, or 3)? 2
Ice Cream Order
Flavor: Chocolate Brownies
Container: Cone
Ingredient: Cookies
Scoops: 2
Total Price: $3.10
Do you want us to remember this order the next
time you come to get your ice scream (y/n)? y
Please enter your initials or the name we will use to remember your order:
Jane44
=-= Ice Cream Vending Machine =-=
Saving Order: Jane44.icr
- Return to the IceCream.vb file