One of themost important tasks in any
programming language is the ability to read and
write files. The steps involved in ASP are no different
than many other languages:
• Specify the location of the file
• Determine if the file exists
• Get a handle to the file
• Read the contents
• Close the file and release any resources used
2.
• File I/Oin ASP can be done using
the FileSystemObject component. When opening a text file
you simply open it as a text stream, and it is this text stream
that you use to access the contents of the file.
• The FileSystemObject allows you to perform all file and folder
handling operations. It can either return a file which can then
be opened as a text stream, or it can return a text stream
object directly.
• Two different methods. The first method gets a file object and
uses that to open the text stream, and the second method
opens the text stream directly from the FileSystemObject.
3.
CreateTextFile Method
The CreateTextFilemethod creates a new text file in the current
folder and returns a TextStream object that can be used to read
from, or write to the file
Syntax
FileSystemObject.CreateTextFile(filename[,overwrite[,unicode]]
Parameter Description
filename Required. The name of the file to create
overwrite Optional. A Boolean value that indicates whether an
existing file can be overwritten. True indicates that the
file can be overwritten and False indicates that the file
can not be overwritten. Default is True
unicode Optional. A Boolean value that indicates whether the file
is created as a Unicode or an ASCII file. True indicates
that the file is created as a Unicode file, False indicates
that the file is created as an ASCII file. Default is False
FileExists Method
Parameter Description
filenameRequired. The name of the file to check if
exist
The FileExists method returns a Boolean value that indicates
whether a specified file exists. It returns True if the file exists and
False if not.
Syntax
FileSystemObject.FileExists(filename)
OpenTextFile Method
The OpenTextFilemethod opens a specified file and returns a
TextStream object that can be used to access the file.
Syntax
FileSystemObject.OpenTextFile(fname,mode,create,format)
Parameter Description
fname Required. The name of the file to open
mode Optional. How to open the file1=ForReading - Open a
file for reading. You cannot write to this file.
2=ForWriting - Open a file for writing.
8=ForAppending - Open a file and write to the end of
the file.
create Optional. Sets whether a new file can be created if the
filename does not exist. True indicates that a new file
can be created, and False indicates that a new file will
not be created. False is default
The asp TextStreamObject allows you to access
the contents of the file as a text file.Once you
have access to the file you can read write
information.Using this you can access the files
with .txt ,.html, .asp ,or.log extensions.
There are three ways that we can crate a text
stream object.
10.
METHOD SYNTAX DESCRIPTION
Read
obj.Read(NoOfCha
r)
Readsa specified
number of characters
from a TextStream file
and returns the
resulting string
ReadLine obj.ReadLine
Reads an entire line (up
to, but not including,
the newline character)
from a TextStream file
and returns the
resulting string.
ReadAll obj.ReadAll
Reads an entire
TextStream file and
returns the resulting
11.
METHOD SYNTAX DESCRIPTION
Writeobj.Write(string)
Writes a specified string
to a TextStream file
WriteLine
obj.WriteLine(stri
ng)
Writes a specified string
and newline character
to a TextStream
file.string is optional.If
omitted, a newline
character is written to
the file.
WriteBlan
kLines
obj.WriteBlankLin
es(lines)
Writes a specified
number of newline
characters to a
12.
METHOD SYNTAX DESCRIPTION
Read
obj.Read(NoOfCha
r)
Readsa specified
number of characters
from a TextStream file
and returns the
resulting string
ReadLine obj.ReadLine
Reads an entire line (up
to, but not including,
the newline character)
from a TextStream file
and returns the
resulting string.
ReadAll obj.ReadAll
Reads an entire
TextStream file and
returns the resulting
13.
METHOD SYNTAX DESCRIPTION
Skip
obj.Skip(NoOfCha
r)
Skipa specified number
of characters from a
TextStream file
SkipLine obj.SkipLine
Skips an entire line (up
to, but not including,
the newline character)
from a TextStream file
14.
Property Description
AtEndOfLine Returnstrue if the file pointer is positioned
immediately before the end-of-line marker in a
TextStream file, and false if not
AtEndOfStream Returns true if the file pointer is at the end of a
TextStream file, and false if not
Column Returns the column number of the current
character position in an input stream
Line Returns the current line number in a
TextStream file
15.
<% dim fs,f,t,x
setfs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.CreateTextFile("c:test.txt")
f.write("Hello World!")
f.Close
set t=fs.OpenTextFile("c:test.txt",1,false)
do while t.AtEndOfLine<>true
x=t.Read(1)
loop
t.close
Response.Write("The last character is: " & x) %>
Output:
The last character of the first line in the text file is: !