Print、Printline 函数

更新:2007 年 11 月

将格式化的显示数据写入顺序文件。

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

参数

  • FileNumber
    必需。任何有效文件号。

  • Output
    可选。要写入文件中的零个或多个用逗号分隔的表达式。

    Output 参数设置包括:

设置

说明

SPC(n)

用于在输出中插入空白字符,其中 n 是要插入的空白字符数。

TAB(n)

用于将插入点定位到某一绝对列号上,其中 n 是列号。使用不带参数的 TAB 将插入点定位在下一打印区的起始位置。

expression

要打印的数值表达式或字符串表达式。

异常

异常类型

错误号

条件

IOException

54

文件模式无效。

IOException

52

FileNumber 不存在。

如果正在升级使用非结构化错误处理方式的 Visual Basic 6.0 应用程序,请参见“错误号”一列。(您可以根据 Number 属性(Err 对象) 比较错误号。) 然而,如果可能,应当考虑用 Visual Basic 的结构化异常处理概述 替换这种错误控制。

备注

提供 Print 和 PrintLine 函数是为了实现向后兼容性,可能会影响性能。对于非旧式应用程序,My.Computer.FileSystem 对象将提供更好的性能。有关更多信息,请参见 使用 Visual Basic 访问文件

Print 在一行的结尾不包括换行符;但是 PrintLine 在一行的结尾包括换行符。

用 Print 写入的数据通常由 LineInput 或 Input 从文件中读取。

对于 PrintLine,如果省略 Output,则向文件打印一个空行;对于 Print,则没有输出。以逗号分隔的多个表达式将以制表符边界为准对齐,但混合使用逗号和 TAB 可能会造成不一致的结果。

对于 Boolean 数据,打印 True 或 False。无论区域设置如何,都不翻译 True 和 False 关键字。

日期数据以系统能够识别的标准短日期格式写入文件。当日期或时间组件丢失或为零时,只向文件写入提供的部分。

如果 Output 数据为空,则不向文件写入任何内容。然而,如果 Output 列表数据是 DBNull,则向文件中写入 Null。

对于 Error 数据,输出显示为 Error errorcode。无论区域设置如何,都不翻译 Error 关键字。

用 Print 写入文件的所有数据都是国际通用的;也就是说,这些数据已用适当的十进制分隔符设置了正确的格式。如果用户希望输出供多个区域设置使用的数据,则应使用 Write。

使用 Print 或 PrintLine 函数写入文件需要有来自 FileIOPermissionAccess 枚举的 Write 访问权限。有关更多信息,请参见 FileIOPermissionAccess 枚举

示例

本示例用 Print 和 PrintLine 函数将数据写入文件。

FileOpen(1, "c:\trash.txt", OpenMode.Output)   ' Open file for output.
Print(1, "This is a test.")   ' Print text to file.
PrintLine(1)   ' Print blank line to file.
PrintLine(1, "Zone 1", TAB(), "Zone 2")   ' Print in two print zones.
PrintLine(1, "Hello", "World")   ' Separate strings with a tab.
PrintLine(1, SPC(5), "5 leading spaces ")   ' Print five leading spaces.
PrintLine(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.
PrintLine(1, aBool, " is a Boolean value")
PrintLine(1, aDate, " is a date")
FileClose(1)   ' Close file.

智能设备开发人员说明

不支持此函数。

要求

命名空间:Microsoft.VisualBasic

**模块:**FileSystem

**程序集:**Visual Basic 运行库(在 Microsoft.VisualBasic.dll 中)

请参见

任务

如何:在 Visual Basic 中向文件内写入文本

如何:在 Visual Basic 中使用 StreamWriter 向文件中写入文本

参考

FileOpen 函数

SPC 函数

TAB 函数

Write、WriteLine 函数

其他资源

使用 Visual Basic 访问文件