Share via


Write、WriteLine 函式

更新:2007 年 11 月

將資料寫入循序檔中。以 Write 寫入的資料,通常都會使用 Input 自檔案中讀取。

Public Sub Write( _
   ByVal FileNumber As Integer, _
   ByVal ParamArray Output As Object _
)
' -or-
Public Sub WriteLine( _
   ByVal FileNumber As Integer, _
   ByVal ParamArray Output() As Object _
)

參數

  • FileNumber
    必要項。包含任何有效檔案號碼的 Integer 運算式。

  • Output
    選擇項。一個或多個以逗號分隔而將寫入檔案的運算式。

例外狀況

例外狀況類型

錯誤代碼

條件

IOException

52

FileNumber 不存在。

IOException

54

檔案模式無效。

如果將使用非結構化錯誤處理的 Visual Basic 6.0 應用程式升級,請參閱「錯誤代碼」資料行 (您可以將錯誤代碼與 Number 屬性 (Err 物件) 比對)。但是,請盡可能考慮以 Visual Basic 的結構化例外處理概觀取代這類錯誤控制項。

備註

提供 Write 和 WriteLine 函式以取得回溯相容性 (Backward Compatibility),但可能會影響效能。對於非舊版應用程式,My.Computer.FileSystem 物件會提供更好的效能。如需詳細資訊,請參閱使用 Visual Basic 存取檔案

如果省略了 Output,則會將空白行列印至檔案。可以用逗號區隔多個運算式。

Write 函式與 Print 函式不同,它會在字串寫入檔案中時,在字串周圍的項目和引號之間插入逗號。您不需要在清單中放入明確的分隔符號。使用 Write 將資料寫入檔案時,只支援數字、Boolean、日期、null 和 Error 資料格式。會遵循下列通用的假設,如此不論地區設定 (Locale) 為何,都能讀取資料並使用 Input 正確解譯:

  • 寫入的數值資料使用句號做為小數點。

  • 對於 Boolean 資料,會印出 #TRUE# 或 #FALSE#。True 和 False 關鍵字不會被轉譯,不論地區設定為何。

  • 會使用通用的日期格式,將日期資料寫入檔案中。當日期或時間元件遺漏或為零時,只會將提供的部分寫入檔案。

  • 如果 Output 資料是空的,就不會將任何資料寫入檔案中。但是,如果是 null 資料,則會寫入 #NULL#。

  • 對於 Error 資料,輸出將出現為 #ERROR errorcode#。不論地區設定為何,都不會轉譯 Error 關鍵字。

WriteLine 會在 Output 的最後一個字元寫入檔案後,插入新行字元 (Newline Character) (也就是,歸位/換行字元或 Chr(13) + Chr(10))。

在字串中,可以用雙引號或 "" 嵌入引號。例如,

Dim x As String = "Double quotation marks aren't ""difficult"" to handle."

傳回含有 Double quotation marks aren't "difficult" to handle 值的字串。

使用 Write 或 WriteLine 函式寫入檔案時,需要 FileIOPermissionAccess 列舉型別的 Append 存取權限。如需詳細資訊,請參閱 FileIOPermissionAccess 列舉型別

範例

這個範例使用 Write 函式將未經處理資料 (Raw Data) 寫入循序檔中。

FileOpen(1, "TESTFILE", OpenMode.Output) ' Open file for output.
Write(1, "This is a test.")  ' Print text to file.
WriteLine(1)  ' Print blank line to file.
WriteLine(1, "Zone 1", TAB(), "Zone 2")   ' Print in two print zones.
WriteLine(1, "Hello", " ", "World")     ' Separate strings with space.
WriteLine(1, SPC(5), "5 leading spaces ")    ' Print five leading spaces.
WriteLine(1, TAB(10), "Hello")   ' Print word at column 10.

' Assign Boolean, Date, and Error values.
Dim aBool As Boolean
Dim aDate As DateTime
aBool = False
aDate = DateTime.Parse("February 12, 1969")

' Dates and Booleans are translated using locale settings of 
' your system.
WriteLine(1, aBool, " is a Boolean value")
WriteLine(1, aDate, " is a date")
FileClose(1)   ' Close file.

智慧型裝置開發人員注意事項

不支援這個函式。

需求

命名空間Microsoft.VisualBasic

**模組︰**FileSystem

組件:Visual Basic Runtime Library (在 Microsoft.VisualBasic.dll 中)

請參閱

工作

HOW TO:在 Visual Basic 中將文字寫入檔案

HOW TO:在 Visual Basic 中以 StreamWriter 將文字寫入檔案

參考

Input 函式

FileOpen 函式

Print、PrintLine 函式

其他資源

使用 Visual Basic 存取檔案