?
This document uses PHP Chinese website manual Release
JScript? | 語言參考 |
有兩種主要的文件處理類型:
創(chuàng)建空文本文件(有時(shí)被叫做“文本流”)有三種方法。
第一種方法是用 CreateTextFile 方法。 下面的示例示范了在 VBScript 中如何用這種方法來創(chuàng)建文本文件:
Dim fso, f1
Set fso = CreateObject("Scripting.FileSystemObject")
Set f1 = fso.CreateTextFile("c:\testfile.txt", True)
要在 JScript 中用這種方法,則使用下面的代碼:
var fso, f1;
fso = new ActiveXObject("Scripting.FileSystemObject");
f1 = fso.CreateTextFile("c:\\testfile.txt", true);
創(chuàng)建文本文件的第二種方法是,使用 FileSystemObject 對(duì)象的 OpenTextFile 方法,并設(shè)置 ForWriting 標(biāo)志。在 VBScript 中,代碼就像下面的示例一樣:
Dim fso, ts
Const ForWriting = 2
Set fso = CreateObject("Scripting. FileSystemObject")
Set ts = fso.OpenTextFile("c:\test.txt", ForWriting, True)
要在 JScript 中使用這種方法來創(chuàng)建文本文件,則使用下面的代碼:
var fso, ts;
var ForWriting= 2;
fso = new ActiveXObject("Scripting.FileSystemObject");
ts = fso.OpenTextFile("c:\\test.txt", ForWriting, true);
創(chuàng)建文本文件的第三種方法是,使用 OpenAsTextStream 方法,并設(shè)置 ForWriting 標(biāo)志。要使用這種方法,在 VBScript 中使用下面的代碼:
Dim fso, f1, ts
Const ForWriting = 2
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CreateTextFile ("c:\test1.txt")
Set f1 = fso.GetFile("c:\test1.txt")
Set ts = f1.OpenAsTextStream(ForWriting, True)
在 JScript 中,則使用下面示例中的代碼:
var fso, f1, ts;
var ForWriting = 2;
fso = new ActiveXObject("Scripting.FileSystemObject");
fso.CreateTextFile ("c:\\test1.txt");
f1 = fso.GetFile("c:\\test1.txt");
ts = f1.OpenAsTextStream(ForWriting, true);
一旦創(chuàng)建了文本文件,使用下面的三個(gè)步驟向文件添加數(shù)據(jù):
打開文本文件。
寫入數(shù)據(jù)。
關(guān)閉文件。
要打開現(xiàn)有的文件,則使用 FileSystemObject 對(duì)象的 OpenTextFile 方法或 File 對(duì)象的 OpenAsTextStream 方法。
要寫數(shù)據(jù)到打開的文本文件,則根據(jù)下表所述任務(wù)使用 TextStream 對(duì)象的 Write、WriteLine 或 WriteBlankLines 方法。
任務(wù) | 方法 |
---|---|
向打開的文本文件寫數(shù)據(jù),不用后續(xù)一個(gè)新行字符。 | Write |
向打開的文本文件寫數(shù)據(jù),后續(xù)一個(gè)新行字符。 | WriteLine |
向打開的文本文件寫一個(gè)或多個(gè)空白行。 | WriteBlankLines |
要關(guān)閉一個(gè)打開的文件,則使用 TextStream 對(duì)象的 Close 方法。
注意??新行字符包含一個(gè)或幾個(gè)字符(取決于操作系統(tǒng)),以把光標(biāo)移動(dòng)到下一行的開始位置(回車/換行)。注意某些字符串末尾可能已經(jīng)有這個(gè)非打印字符了。
下面的 VBScript 例子示范了如何打開文件,和同時(shí)使用三種寫方法來向文件添加數(shù)據(jù),然后關(guān)閉文件:
Sub CreateFile()
???Dim fso, tf
???Set fso = CreateObject("Scripting.FileSystemObject")
???Set tf = fso.CreateTextFile("c:\testfile.txt", True)
???'
寫一行,并且?guī)в行滦凶址? ???tf.WriteLine("Testing 1, 2, 3.")
???'
向文件寫三個(gè)新行字符。??????
???tf.WriteBlankLines(3)
???'
寫一行。 ???tf.Write ("This is a test.")
???tf.Close
End Sub
這個(gè)示例示范了在 JScript 中如何使用這三個(gè)方法:
function CreateFile() {
???var fso, tf;
???fso = new ActiveXObject("Scripting.FileSystemObject");
???tf = fso.CreateTextFile("c:\\testfile.txt", true);
???//
寫一行,并且?guī)в行滦凶址? ???tf.WriteLine("Testing 1, 2, 3.") ;
???//
向文件寫三個(gè)新行字符。???
tf.WriteBlankLines(3) ;
???//
寫一行。 ???tf.Write ("This is a test.");
???tf.Close();
}
要從文本文件讀取數(shù)據(jù),則使用 TextStream 對(duì)象的 Read、ReadLine 或 ReadAll 方法。下表描述了不同的任務(wù)應(yīng)使用哪種方法。
任務(wù) | 方法 |
---|---|
從文件讀取指定數(shù)量的字符。 | Read |
讀取一整行(一直到但不包括新行字符)。 | ReadLine |
讀取文本文件的整個(gè)內(nèi)容。 | ReadAll |
如果使用 Read 或 ReadLine 方法,并且想跳過數(shù)據(jù)的特殊部分,則使用 Skip 或 SkipLine 方法。read 方法的結(jié)果文本存在一個(gè)字符串中,該字符串可以顯示在一個(gè)控件中,也可以用字符串函數(shù)(如 Left、Right 和 Mid)來分析,連接等等。
下面的 VBScript 示例示范了如何打開文件,和如何寫數(shù)據(jù)到文件中并從文件讀取數(shù)據(jù):
Sub ReadFiles
???Dim fso, f1, ts, s
???Const ForReading = 1
???Set fso = CreateObject("Scripting.FileSystemObject")
???Set f1 = fso.CreateTextFile("c:\testfile.txt", True)
???'
寫一行。 ???Response.Write "Writing file <br>"
???f1.WriteLine "Hello World"
???f1.WriteBlankLines(1)
???f1.Close
???'
讀取文件的內(nèi)容。 ???Response.Write "Reading file <br>"
???Set ts = fso.OpenTextFile("c:\testfile.txt", ForReading)
???s = ts.ReadLine
???Response.Write "File contents = '" & s & "'"
???ts.Close
End Sub
下面的代碼示范了在 JScript 中做同樣的事:
function ReadFiles() {
???var fso, f1, ts, s;
???var ForReading = 1;
???fso = new ActiveXObject("Scripting.FileSystemObject");
???f1 = fso.CreateTextFile("c:\\testfile.txt", true);
???//
寫一行。 ???Response.Write("Writing file <br>");
???f1.WriteLine("Hello World");
???f1.WriteBlankLines(1);
???f1.Close();
???//
讀取文件的內(nèi)容。 ???Response.Write("Reading file <br>");
???ts = fso.OpenTextFile("c:\\testfile.txt", ForReading);
???s = ts.ReadLine();
???Response.Write("File contents = '" + s + "'");
???ts.Close();
}
FSO 對(duì)象模式各有兩種方法移動(dòng)、復(fù)制和刪除文件,如下表所述。
任務(wù) | 方法 |
---|---|
移動(dòng)文件 | File.Move 或 FileSystemObject.MoveFile |
復(fù)制文件 | File.Copy 或 FileSystemObject.CopyFile |
刪除文件 | File.Delete 或 FileSystemObject.DeleteFile |
下面的 VBScript 示例,在驅(qū)動(dòng)器 C 的根目錄中創(chuàng)建一個(gè)文本文件,向其中寫一些信息,然后把它移動(dòng)到 \tmp 目錄中,并在 \temp 中做一個(gè)備份,最后把它們從兩個(gè)目錄中刪掉。
要運(yùn)行下面的示例,需要先在驅(qū)動(dòng)器 C 的根目錄中創(chuàng)建 \tmp 和 \temp 目錄:
Sub ManipFiles
???Dim fso, f1, f2, s
???Set fso = CreateObject("Scripting.FileSystemObject")
???Set f1 = fso.CreateTextFile("c:\testfile.txt", True)
???Response.Write "Writing file <br>"
???'
寫一行。 ???f1.Write ("This is a test.")
???'
關(guān)閉文件。 ???f1.Close
???Response.Write "Moving file to c:\tmp <br>"
???'
獲取C
的根目錄(C:\)
中的文件的句柄。 ???Set f2 = fso.GetFile("c:\testfile.txt")
???'
把文件移動(dòng)到\tmp
目錄。 ???f2.Move ("c:\tmp\testfile.txt")
???Response.Write "Copying file to c:\temp <br>"
???'
把文件復(fù)制到\temp
目錄。 ???f2.Copy ("c:\temp\testfile.txt")
???Response.Write "Deleting files <br>"
???'
獲得文件當(dāng)前位置的句柄。 ???Set f2 = fso.GetFile("c:\tmp\testfile.txt")
???Set f3 = fso.GetFile("c:\temp\testfile.txt")
???'
刪除文件。 ???f2.Delete
???f3.Delete
???Response.Write "All done!"
End Sub
下面的代碼示范了在 JScript 中做同樣的事:
function ManipFiles() {
???var fso, f1, f2, s;
???fso = new ActiveXObject("Scripting.FileSystemObject");
???f1 = fso.CreateTextFile("c:\\testfile.txt", true);
???Response.Write("Writing file <br>");
???//
寫一行。 ???f1.Write("This is a test.");
???//
關(guān)閉文件。 ???f1.Close();
???Response.Write("Moving file to c:\\tmp <br>");
???//
獲取C
的根目錄(C:\)
中的文件的句柄。 ???f2 = fso.GetFile("c:\\testfile.txt");
???//
把文件移動(dòng)到\tmp
目錄。 ???f2.Move ("c:\\tmp\\testfile.txt");
???Response.Write("Copying file to c:\\temp <br>");
???//
把文件復(fù)制到\temp
目錄。 ???f2.Copy ("c:\\temp\\testfile.txt");
???Response.Write("Deleting files <br>");
???//
獲得文件當(dāng)前位置的句柄。 ???f2 = fso.GetFile("c:\\tmp\\testfile.txt");
???f3 = fso.GetFile("c:\\temp\\testfile.txt");
???//
刪除文件。 ???f2.Delete();
???f3.Delete();
???Response.Write("All done!");
}