In the following example, we will create a new text file Test.txt and write some text. First, we create an instance of the FileSystemObject object and then use the CreateTextFile method to create a new text file. CreateTextFile returns a TextStream object that we will use to write some text files.
<%
Dim objFSO, Objtstream
' Create An instance of the FileSystemObject object
Set objFSO = Server.createo Bject ("Scripting.FileSystemObject")
' Create a text file named MyFile.txt, replacing any existing one with the same Nam E
Set objtstream = objFSO.CreateTextFile ("C:est.txt", True)
' Write some text to the ' file
Objtstream.writeline ("Webcheatsheet ASP tutorials!")
Objtstream.writeblanklines (1)
Objtstream.writeline ("The TextStream Object")
Objtstream.writeblanklines (2)
Objtstream.writeline ("The TextStream object provides sequential access to the contents of text files.")
' Close the TextStream object
Objtstream.close
' free to resources
Set objtstream = Nothing
Set objFSO = Nothing
%>
In the following example, we will carefully read the contents of the Test.txt file. First, we create an instance of the FileSystemObject object and then use the OpenTextFile method to open the Test.txt file for reading. This method returns a TextStream object that we will use to read data from the file. We'll walk through each reading of a row of files.
<%
Dim objFSO, Objtstream
' Create An instance of the FileSystemObject object
Set objFSO = Server.createo Bject ("Scripting.FileSystemObject")
' opens for reading a text file named MyFile.txt
Set objtstream = Objfso.opente Xtfile ("C:est.txt", 1)
' Read one line at a time until the ' the ' the ' the ' the ' the ' the ' of ' the ' the ' of the ' of ' Tream
Response.Write "line" & Objtstream.line & ":" & Objtstream.readline & "<br/>"
Loop
' close ' TextStream object
Objtstream.close
' free to resources
Set objtstream = Nothing
Set o Bjfso = Nothing
%>