Reading and Writing to Files on the Intranet - Opening the Stream Object (Page 2 of 5 )
In the previous article a couple of ways to open a stream object were described with examples as shown. Opening the Stream object with the URL will be used in this tutorial. However, opening the Stream object without any argument offers you the possibility of accessing elements not related to the intranet. Here are some of the ways you can open a Stream object:
- Record opens with an open recordset and Stream uses the open Record.
- Open a Stream using an open Record Object.
- Open a Stream using an URL reference.
- Open a Stream with no argument by using the Memory Stream.
Reading and writing text with the Stream Object
While the Record object deals with accessing and manipulating folders and files, the Stream object deals with what is contained in the files. The Record object can create files as described in a previous tutorial, and the Stream object can access, read from and write to these files.
The Stream object deals with two kinds of file content, text and binary. For this purpose it has several properties and methods which were not covered in the basic tutorial on the Stream object. They will be described with examples here.
Reading a text file on the intranet
The ReadText() method reads a specified number of characters from a Stream object working with a text. The related Type for a stream object is adTypeText. Stream.ReadText(NumChars) will return a string. It can therefore read a number of characters in the stream; an entire line; or the whole content in the stream. If no argument is specified, it reads the entire file.
An example of reading the text
The text to be read is on an intranet file, the content of which is shown in the next paragraph.
Test.text
It must be remembered that there is nothing more difficult to plan,
more difficult of success, nor more dangerous to plan than the
creation of a new System.
The code listing shown in the next paragraph reads the text in the above file and the portion that is read is printed to the immediate screen by the debug statements.
Option Compare Database
Dim strm As ADODB.stream
Private Sub Command0_Click()
strm = New ADODB.stream
strm.Charset = "ASCII"
strm.Type = adTypeText
Debug.Print "Stream Open"
strm.Open "url=http://hodentek/DevHome/ASPFree/Test.txt", adModeReadWrite
'Reading the text here
Debug.Print "---------------reads a single line from the stream-----"
Debug.Print strm.ReadText(ADODB.StreamReadEnum.adReadLine)
Debug.Print "-------------------------------------------------------"
Debug.Print "------------------read 6 characters-------------"
Debug.Print strm.ReadText(6)
Debug.Print "----------------------------------------------"
Debug.Print "------------------read 10 characters--------------------"
Debug.Print strm.ReadText(10)
Debug.Print "-----------------------------------------------"
'Stream.EOS indicates whether or not the stream has ended
'if some content is still in left in the stream the End Of Stream(EOS) is
'false
MsgBox(strm.EOS)
Debug.Print strm.ReadText()
Debug.Print "-----------------------------------------------"
strm.Close
Debug.Print "Stream Closed"
Debug.Print "-------------------------" & vbCrLf
Set strm = Nothing
End Sub
The result of the above reading is printed to the immediate window as shown below.
Stream Open
---------------reads a single line from the stream-----
It must be remembered that there is nothing more difficult to plan,
-------------------------------------------------------
------------------read 6 characters-------------
more d
----------------------------------------------
------------------read 10 characters--------------------
ifficult of
-----------------------------------------------
success, nor more dangerous to plan than the
creation of a new System -----------------------------------------------
Stream Closed
-------------------------
When you call the ReadText() method without any argument as in the previous listing, the End of File becomes true as what is left of the stream is completely read by the last read, stream.ReadText()..
Next: Can you read text in another language? >>
More Database Articles
More By Jayaram Krishnaswamy