|
|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Now that weve created a TextStream, we can write to it, as well see later in this chapter. Opening A TextStreamTo open a TextStream, you use the FileSystemObjects OpenTextFile method: FileSystemObject.OpenTextFile( filename[, iomode[, create[, format]]]) Here are what the arguments to OpenTextFile mean:
Heres an example where we open a TextStream object corresponding to a file named file.txt:
Private Sub Command2_Click()
Dim FileSystemObject, TextStream As Object
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
Set TextStream = FileSystemObject.OpenTextFile("c:\file.txt")
End Sub
After youve opened a TextStream object, you can read from it, as well see later in this chapter. Writing To A TextStreamTo write to a TextStream object, you use one of these methods: Write( string) WriteLine([ string]) Heres an example where we create a file named file.txt and write a string, Here is some text! to that file. First, we create a new TextStream:
Private Sub Command1_Click()
Dim FileSystemObject, TextStream As Object
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
Set TextStream = FileSystemObject.CreateTextFile("c:\file.txt", True)
...
Then we write our line of text to the file and close that file:
Private Sub Command1_Click()
Dim FileSystemObject, TextStream As Object
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
Set TextStream = FileSystemObject.CreateTextFile("c:\file.txt", True)
TextStream.WriteLine ("Here is some text!")
TextStream.Close
End Sub
Reading From A TextStreamTo read from a TextStream object, you use one of these methods; note that the Read method lets you specify how many characters to read: Read( numbercharacters) ReadAll ReadLine Each of these methods returns the text read. Lets see an example. In this case, well open a file, file.txt, and read one line from it, displaying that line in a text box. First, we create a TextStream object for that file:
Private Sub Command1_Click()
Dim FileSystemObject, TextStream As Object
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
Set TextStream = FileSystemObject.OpenTextFile("c:\file.txt")
...
Next, we use the ReadLine method to read a line from the file and display it in a text box, Text1, and close the TextStream:
Private Sub Command1_Click()
Dim FileSystemObject, TextStream As Object
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
Set TextStream = FileSystemObject.OpenTextFile("c:\file.txt")
Text1.Text = TextStream.ReadLine
TextStream.Close
End Sub
Closing A TextStreamWhen youre finished working with a TextStream object, you close it using the Close method. In the following example, we write to a file, file.txt, using a TextStream object and then close that TextStream (and therefore the file) using Close (this method takes no arguments):
Private Sub Command1_Click()
Dim FileSystemObject, TextStream As Object
Set FileSystemObject = CreateObject("Scripting.FileSystemObject")
Set TextStream = FileSystemObject.CreateTextFile("c:\file.txt", True)
TextStream.WriteLine ("Here is some text!")
TextStream.Close
End Sub
|
|
Products | Contact Us | About Us | Privacy | Ad Info | Home
Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. |