Chapter 52: ASP TextStream Object

1. What is the TextStream Object?

TextStream is not created directly with Server.CreateObject.

You get a TextStream object when you call one of these methods on a FileSystemObject:

  • fso.OpenTextFile(path, mode, create, format)
  • fso.CreateTextFile(path, overwrite, unicode)

It represents an open text file stream — you can:

  • Read the entire file at once (.ReadAll)
  • Read line by line (.ReadLine, .Read(n))
  • Write text (.Write, .WriteLine)
  • Skip lines (.Skip, .SkipLine)
  • Check end of file (.AtEndOfStream)
  • Close it when done (.Close)

Important modes (second parameter of OpenTextFile):

Mode constant Value Meaning Typical use
ForReading 1 Open for reading only Read config, logs, data files
ForWriting 2 Open for writing (overwrites) Create new log, report, export
ForAppending 8 Open for writing (adds to end) Append to log file (most common)

Unicode/ASCII flag (fourth parameter of OpenTextFile):

  • True = Unicode (UTF-16)
  • False = ASCII/ANSI (default, most common in old ASP)

2. Step-by-Step Example 1 – Read Entire File & Display It

asp

welcome.txt content:

text

→ Browser shows the file content safely escaped inside <pre>.

3. Example 2 – Read Line by Line (Very Common for CSV/Logs)

asp

→ This is how many old sites displayed log files or simple CSV imports.

4. Example 3 – Write / Append to Log File (Most Common Real Use)

asp

→ Every page hit appends one line — classic simple analytics/logging pattern.

5. Security & Best Practice Warnings (Critical in 2026)

  • Never allow users to control the path passed to OpenTextFile or CreateTextFile — classic ASP sites were hacked constantly via path traversal (../../etc/passwd) or writing malicious files to web folders.
  • Disable write permissions to web folders in IIS if possible — many hosts block FSO write access now.
  • Always use Server.MapPath — never hard-code physical paths (C:\inetpub\…).
  • Always close TextStream (ts.Close) and set to Nothing.
  • Use On Error Resume Next around file operations + check Err.Number.
  • Log FSO errors — disk full, permission denied, etc. were common silent failures.
  • Never expose raw file content without Server.HTMLEncode — even log files can contain user input.

6. Teacher Summary – ASP TextStream Object in Classic ASP

ASP TextStream Object is:

  • Created by fso.OpenTextFile(path, mode, create) or fso.CreateTextFile(path)
  • Represents an open text file for reading or writing
  • Main methods: .ReadAll, .ReadLine, .Write, .WriteLine, .Close, .AtEndOfStream
  • Modes: 1=ForReading, 2=ForWriting, 8=ForAppending
  • Used for: reading config/logs/CSV, writing logs/reports/exports, appending visit data
  • Always use Server.MapPath for path
  • AlwaysServer.HTMLEncode when displaying file content
  • Always close stream (ts.Close) and set Nothing
  • Very powerful — but extremely dangerous if write access is not locked down

This is how millions of Classic ASP sites read config files, wrote logs, generated reports, imported CSV, and appended audit trails — and many legacy Indian systems still use exactly this TextStream pattern in 2026.

Next class?

  • Want a full example combining FSO + TextStream + upload form?
  • Or how to read CSV line by line and parse into table?
  • Or secure FSO/TextStream usage (permissions, path validation)?
  • Or move to the next W3Schools topic (ASP Cookies or ASP Sessions)?

Just tell me — I’m here! 🚀🇮🇳 Keep learning strong, Webliance! 😊

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *