|
|
Fun with the StreamWriterExamples 1-4 | | .NET has some really cool support for many things, among which are Streams. A Stream is ultimately derived from the System.IO.Stream Class. What I want to do is show you a few cool tricks that you can do with Streams.
The first thing you need to do when writing a Stream is deciding where it's going to go. One of the things .NET makes quite simple is writing to Windows Temp Files. After that, the rest is easy. Let's walk through some common tasks that you can perform using System.IO
1-Writing Data to a Temp File
Dim myWriter as StreamWriter
Dim myStream as FileStream
myStream = New FileStream(Path.GetTempFileName(), FileMode.Create)
myWriter = New StreamWriter(myStream)
With myWriter
.Write(".NET is really Cool" & Environment.NewLine)
.Write("Environment is a really cool namespace")
.Write("There's a lot of neat stuff you can do")
.Write("I'm writing this " & DateTime.Now.ToShortDateString)
.Close()
End With
myStream.Close() |
2-Writing the Contents of a TextBox or RichTextBox
Dim myWriter as StreamWriter
Dim myStream as FileStream
myStream = New FileStream(Path.GetTempFileName(), FileMode.Create)
myWriter = New StreamWriter(myStream)
myWriter.Write(tbMyTextBox.text)
myWriter.Write(rtMyRichTextBox.text)
myWriter.Close()
myStream.Close() |
*** At this point, you should be noticing that to implement a class that took care of all of this for you, you could create a Shared/Static method that encapsulated this. Just pass in the filename of the file you want to create/write and a string or array of string with what you want to write
3- Writing a Strongly Typed Collection to a TextFile
Dim myWriter as StreamWriter
Dim myStream as FileStream
myStream = New FileStream(Path.GetTempFileName(), FileMode.Create)
myWriter = New StreamWriter(myStream)
Dim myItem as Object
For Each myItem in SomeObject.Items
myWriter.Write(myItem.ToString)
Next
myWriter.Close()
myStream.Close() |
*** Here you could write whatever property you wanted. In this instance, I'm writing everything contained in the object's .ToString() method but this doesn't have to be the case. For instance, you could do the same thing with a Controls Collection and write out the Control's Name Property for instance. I'm using the simplified .ToString() method b/c every object has one, but there's a whole lot more you could write out.
4 - Read from a TextFile
Dim myReader as StreamReader
Dim myStream as FileStream
Dim al as New ArrayList
myStream = New FileStream("C:\YourFileNameHere.txt", FileMode.Create)
myReader = New StreamWriter(myStream)
Dim myItem as Object
myItem = myReader.ReadLine() ' Reads line by line breaking at ControlChars.CrLf
While Not myItem = Nothing
al.Add(myItem) 'We're just adding a text object into an arraylist
' but you can do a whole lot more with a little creativity
End While
myReader.Close()
myStream.Close() |
Can it really be this easy? Yes and No. Writing code exactly like this would be pretty dangerous because I'm assuming that I can access the file each time, whether I'm reading from it or writing to it. That's not often a safe assumption in practice. Moreover, you may want to Delete the file if it already exists, append to it or whatever else. In addition, you can see that for the first three examples, the first few lines are identical. Hopefully none of my college professors are looking at those examples without reading this because, well, they'd probably hand me a Cobol compiler and make me work on the AS400. I can hear it now "If you don't want to embrace OOP, there's a place for you in the professional world, TYPING INTO A GREEN SCREEN!") My point being that since many things you do with streams will is similar, this class lends itself Very well to creating either subclasses (if you want to be cool) or utility/helper classes to handle most of the declarations. After all, as easy as this stuff is to use, it'd certainly be easier to simply pass in a file name and what you want to write.
Now for the good plug for my next article. Writing to Files is Lame! Streams are cool and should be written to Sockets, at least most of the time. Anyway, in examples 5-10, I'm going to turn it up a notch and incorporate System.IO with System.Net and show some much more sophisticated techniques for writing data... |
|