Sub CreateFile() Dim fso, tf Set fso = CreateObject("Scripting.FileSystemObject") Set tf = fso.CreateTextFile("c:\testfile.txt", True) ' 写一行,并且带有新行字符。 tf.WriteLine("Testing 1, 2, 3.") ' 向文件写三个新行字符。 tf.WriteBlankLines(3) ' 写一行。 tf.Write ("This is a test.") tf.Close End Sub 这个示例示范了在 JScript 中如何使用这三个方法:
function CreateFile() { var fso, tf; fso = new ActiveXObject("Scripting.FileSystemObject"); tf = fso.CreateTextFile("c:\\testfile.txt", true); // 写一行,并且带有新行字符。 tf.WriteLine("Testing 1, 2, 3.") ; // 向文件写三个新行字符。 tf.WriteBlankLines(3) ; // 写一行。 tf.Write ("This is a test."); tf.Close(); } 读取文件 要从文本文件读取数据,则使用 TextStream 对象的 Read、ReadLine 或 ReadAll 方法。下表描述了不同的任务应使用哪种方法。 任务 方法 从文件读取指定数量的字符。 Read 读取一整行(一直到但不包括新行字符)。 ReadLine 读取文本文件的整个内容。 ReadAll