The ASP OpenTextFile Method is used to open a specified file. It returns a TextStream object that can be used to read, write and append contents to the File. It is an in-built function of the FileSystem Object.
Syntax:
FileSystemObject.OpenTextFile(fname,mode,create,format)
Parameter Value:
- fname: Required attribute. It specifies the name of the file to be opened.
- mode: It is an optional attribute. It specifies that how to open a file. It contains 3 constant values to perform an operation on a file.
- 1 for reading the contents.
- 2 open a file for writing the contents
- 8 for appending the contents.
- create: Optional Attribute. It contains a Boolean value that specifies that whether there can be created if the specified filename doesn't exist.
- The value is True if a new file is created;
- false if it isn't created. The default is False.
- Format: Optional attribute. It contains three constant value which used to define a format of a file
- 0=TristateFalse - Open the file as ASCII. This is the default.
- -1=TristateTrue - Open the file as Unicode.
- -2 =TristateUseDefault - Open the file using the system default.
Example Code: Below code illustrates that how to open a new file for writing the content.
<%
dim fs,f
set fs=Server.CreateObject("Scripting.FileSystemObject")
set f=fs.OpenTextFile(Server.MapPath("GFG.txt"),2,true)
response.write("File is open for purpose of writing")
f.WriteLine("This text will be added to the end of file")
f.Close
set f=Nothing
set fs=Nothing
%>
Output:
File is open for purpose of writing