Write, WriteLine Functions

Writes data to a sequential file. Data written with Write is usually read from a file with 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 _
)

Parameters

  • FileNumber
    Required. An Integer expression containing any valid file number.

  • Output
    Optional. One or more comma-delimited expressions to write to a file.

Exceptions

Exception type

Error number

Condition

IOException

52

FileNumber does not exist.

IOException

54

File mode is invalid.

See the "Error number" column if you are upgrading Visual Basic 6.0 applications that use unstructured error handling. (You can compare the error number against the Number Property (Err Object).) However, when possible, you should consider replacing such error control with Structured Exception Handling Overview for Visual Basic.

Remarks

The Write and WriteLine functions are provided for backward compatibility and may have an impact on performance. For non-legacy applications, the My.Computer.FileSystem object provides better performance. For more information, see File Access with Visual Basic.

If you omit Output, a blank line is printed to the file. Multiple expressions can be separated with a comma.

Unlike the Print function, the Write function inserts commas between items and quotation marks around strings as they are written to the file. You do not have to put explicit delimiters in the list. When Write is used to write data to a file, only numeric, Boolean, date, null, and Error data formats are supported. The following universal assumptions are followed so the data can always be read and correctly interpreted using Input, regardless of locale:

  • Numeric data is always written using the period as the decimal separator.

  • Date data is written to the file using the universal date format. When either the date or the time component is missing or zero, only the part provided gets written to the file.

  • Nothing is written to the file if Output data is empty. However, for null data, #NULL# is written.

  • For Error data, the output appears as #ERROR errorcode#. The Error keyword is not translated, regardless of locale.

WriteLine inserts a newline character (that is, a carriage return/line feed, or Chr(13) + Chr(10)), after it has written the final character in Output to the file.

You can embed quotation marks in a string by using double quotation marks, or "". For example,

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

returns a string with the value of Double quotation marks aren't "difficult" to handle.

Writing to a file with the Write or WriteLine functions requires Append access from the FileIOPermissionAccess enumeration. For more information, see FileIOPermissionAccess Enumeration.

Example

This example uses the Write and WriteLine functions to write raw data to a sequential file.

' Open file for output.
FileOpen(1, "TestFile.txt", OpenMode.Output)
' Print text to the file. The quotation marks will be in the display.
Write(1, "This is a test.")
' Go to the next line.
WriteLine(1)
' Skip a line.
WriteLine(1)
' Print in two print zones. You will see commas and quotation marks 
' in the output file.
WriteLine(1, "Zone 1", SPC(10), "Zone 2")
' Build a longer string before calling WriteLine.
WriteLine(1, "Hello" & "  " & "World")
' Include five leading spaces.
WriteLine(1, SPC(5), "Leading spaces")
' Print a word starting at column 10.
WriteLine(1, TAB(10), "Hello")

' Assign Boolean and Date 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.")
' Close the file.
FileClose(1)

' Contents of TestFile.txt 
'"This is a test.", 
' 
'"Zone 1",          "Zone 2" 
'"Hello  World" 
'     "Leading spaces" 
'         ,"Hello" 
'"False is a Boolean value." 
'"2/12/1969 is a date."

Smart Device Developer Notes

This function is not supported.

Requirements

Namespace: Microsoft.VisualBasic

**Module:**FileSystem

Assembly: Visual Basic Runtime Library (in Microsoft.VisualBasic.dll)

See Also

Tasks

How to: Write Text to Files in Visual Basic

How to: Write Text to Files with a StreamWriter in Visual Basic

Reference

Input Function

FileOpen Function

Print, PrintLine Functions

My.Computer.FileSystem.WriteAllText Method

My.Computer.FileSystem.OpenTextFileWriter Method

Console.Write

Console.WriteLine

Other Resources

File Access with Visual Basic

Change History

Date

History

Reason

August 2008

Updated the example and added additional links to alternative ways to write to text files.

Customer feedback.