Working with the TextStream Object

The TextStream object is the heart of most of the file manipulation in ASP 3.0.  There are three specific methods within the TextStream object: CreateTextFile, OpenTextFile, and OpenAsTextStream.

The TextStream object has its own set of properties and methods. First, the properties:

  • AtEndOfLine. Returns a value of True if the file pointer (the location of the response as the file is read) is at the end of a line in the file

  • AtEndOfStream. Returns a value of True if the file pointer is at the end of the file
  • Column. Depending on the current character being read within the file, returns the column number of this character, starting from 1

  • Line. Depending on the current line number being read within the file, returns this line number, starting from 1

And now, the methods of the TextStream object:

  • Close. Closes an open file.

  • Read (a number). Reads a specific number of characters from the file. For example, Read(20) reads the first 20 characters within the file being examined.

  • ReadAll(). Reads the entire file and places it within a text string.

  • ReadLine(). Reads a single line from the file and places it within a text string.

  • Skip (a number). Skips over a specific set of characters from the file being examined.

  • SkipLine. Skips a line when reading from the file being examined.

  • Write (a string). Writes a specified string to the file being examined.

  • WriteLine (a string). Writes a string to the file being examined and then writes a newline character within the file.

  • WriteBlankLines(a number). Writes the specified number of blank lines to the file being examined.  

The following is an example of the TextStream object in action.  Note the location of where you create the file will vary (as well as the name of the actual text file: in this example, it is called "Test2File.txt"), depending on your particular server configuration.

<html>
<title>Working with the File Object</title>

<body>
<b>An example of working with the File object!</b>

<hr>
<%
set TestFile=Server.CreateObject("Scripting.FileSystemObject")
set TFileStream= TestFile.CreateTextFile(
     "c:\inetpub\wwwroot\ASP_Examples\Test2File.txt")

TFileStream.WriteLine "Welcome to the File Object in ASP!"
TFileStream.WriteBlankLines(3)
TFileStream.WriteLine "Between this line and the opening line are three
    blank lines. These blank lines were inserted using the 
    WriteLine method of the TextStream object. Now, let's 
    write three more blank lines before the next section of
    text is inserted."

TFileStream.WriteBlankLines(3)
TFileStream.WriteLine "Okay, that's better-three more 
     blank lines have just been inserted! I think you probably
     get the idea of how to use the WriteLine method, so let's
     move on to more interesting things."

TFileStream.Close
%>

</body>
</html>

After the page loads, the Test2File.txt file is created.   Navigate to the location of this file -- it should look something like this (all the text you asked to be inserted, including the blank lines is included within this file).



Click here for larger image

The information you write to a text file doesn't have to be static text. You can define and assign specific values to variables and then, as a result of your own code processing, insert dynamic values into the text files. For example, you ask visitors to your Web site to enter specific information. Then, you have your code process that information and write the results of that processing to a text file.

Reading from a Text File

Reading from a file is also very straightforward via the power of the TextStream object.  You can see how this is done with the following code, using the TextStream object's properties and methods.

<html>
<title>Working with the File Object</title>

<body>
<b>A second example of working with the File object!</b>

<hr>

<%
set TestFile=Server.CreateObject("Scripting.FileSystemObject")
set TFileStream=TestFile.OpenTextFile(
    "c:\inetpub\wwwroot\ASP_Examples\TestFile2.txt")
TextFormat=TFileStream.ReadLine

%>
<p>
<font face="Century Gothic" size="5" color="#008000">
<i>
<%=TextFormat%></i>
</font></p>

</body>
</html>

Opening this file in a Web browser, it should appear as it does here



Click here for larger image

The capability to read from an existing text file enables you to draw on all kinds of additional information resources when developing in ASP.

As you can see in this figure, the preceding code opens the TestFile2.txt file for reading. Then (through the power of the ReadLine method) the code reads the first line of text from this file, Welcome to the File Object in ASP!. Remember from the description of the ReadLine method, the data returned is set to a string. Keeping this in mind, you set the value of the ReadLine method to the variable TextFormat. Finally, before outputting the contents of this variable to the screen (via the line <%=TextFormat%>), you apply some general HTML formatting. I set the font to Century Gothic, assigned a bigger font size, and italicized it. This formatting gives the text (as read from the file) its unique appearance, as shown in the figure.


About the Authors

John Gosney is a Web-Database Applications Developer working with ColdFusion, Java, Active Server Pages, and other Web-based technologies. He has designed and implemented complete training courses for systems utilizing computer-based training. John Gosney is the author of several books including Customer Relationship Management Essentials and Web Enable Your Small Business In a Weekend.

This article is brought to you by Premier Press publisher of ASP Programming for the Absolute Beginner
© Copyright Prima Publishing, All Rights Reserved

# # #



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • Due to internal controls and regulations, the amount of long term archival data is increasing every year. Since magnetic tape does not need to be periodically operated or connected to a power source, there will be no data loss because of performance degradation due to the drive actuator. Read this white paper to learn about a series of tests that determined magnetic tape is a reliable long-term storage solution for up to 30 years.

Most Popular Programming Stories

More for Developers

RSS Feeds