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 方法。下表描述了不同的任务应使用哪种方法。