⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.frm

📁 用vb实现读取文本文件(程序里包含三种不同的实现方法)
💻 FRM
字号:
VERSION 5.00
Begin VB.Form Form1 
   Caption         =   "Form1"
   ClientHeight    =   3090
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   4680
   LinkTopic       =   "Form1"
   ScaleHeight     =   3090
   ScaleWidth      =   4680
   StartUpPosition =   3  '窗口缺省
   Begin VB.TextBox Text4 
      Height          =   615
      Left            =   3360
      TabIndex        =   7
      Text            =   "Text4"
      Top             =   4680
      Width           =   4575
   End
   Begin VB.CommandButton Command4 
      Caption         =   "Command4"
      Height          =   375
      Left            =   1680
      TabIndex        =   6
      Top             =   4800
      Width           =   1095
   End
   Begin VB.TextBox Text3 
      Height          =   615
      Left            =   3360
      TabIndex        =   5
      Text            =   "Text3"
      Top             =   3720
      Width           =   4575
   End
   Begin VB.CommandButton Command3 
      Caption         =   "Command3"
      Height          =   375
      Left            =   1680
      TabIndex        =   4
      Top             =   3840
      Width           =   1095
   End
   Begin VB.TextBox Text2 
      Height          =   615
      Left            =   3360
      TabIndex        =   3
      Text            =   "Text2"
      Top             =   2520
      Width           =   4575
   End
   Begin VB.CommandButton Command2 
      Caption         =   "Command2"
      Height          =   375
      Left            =   1680
      TabIndex        =   2
      Top             =   2640
      Width           =   1095
   End
   Begin VB.TextBox Text1 
      Height          =   735
      Left            =   3360
      TabIndex        =   1
      Text            =   "Text1"
      Top             =   1320
      Width           =   4575
   End
   Begin VB.CommandButton Command1 
      Caption         =   "Command1"
      Height          =   495
      Left            =   1680
      TabIndex        =   0
      Top             =   1200
      Width           =   1095
   End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
'方法一:
'用定长的String变量获取文本内容,由于定长String变量支持的下界为65400
'所以在打开超过32K字节的文件时超出部分的字节将无法获取

Dim sA As String * 65400 '声明定长String变量
Open "e:\d.txt" For Binary As #1 '用二进制打开文件
Get #1, , sA '用Get语句从文件中获取字节
Text1 = sA '显示打开的文件
Close #1 '关闭文件

End Sub

Private Sub Command2_Click()
'方法二:
'先声明一字符串变量,然后用空格填充字符串,使变量大小与文件大小一致,
'再通过Get语句将文件全部数据存储到变量中,从而达到获取整个文件字节数的目的。
'此法可以打开大于32K的文件,但应该注意的是,装载文件的容器必须能装载大于32K的文件

Dim sA As String
Open "e:\d.txt" For Binary As #1
sA = Space(LOF(1)) '用空格填充sA变量
Get #1, , sA
Text2.Text = sA
Close #1

End Sub

Private Sub Command3_Click()
'用StrConv函数将文件的控制字符串数据和Unicode码之间进行转换,从而达到打开文件的目的。可打开任意大小文件

Open "e:\d.txt" For Input As #1
Text3.Text = StrConv(InputB$(LOF(1), 1), vbUnicode)
Close #1

End Sub

Private Sub Command4_Click()

Dim Lstr As String
Open "e:\d.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, Lstr
Text4.Text = Text4.tex & Lstr & vbCrLf
Loop
Close #1

End Sub

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -