VB學(xué)習(xí):文本插入與編輯處理實(shí)例

字號:

VB操作文本文件的方法很多,下面的例子是我自己作項(xiàng)目或者回答網(wǎng)友提問時(shí)做的,很有代表性,希望能夠給各位朋友一些啟發(fā).
     '功能:刪除、替換文本中一行,或者插入內(nèi)容到文本中某一行
     '作者: soho_andy (冰)
     '參數(shù):
     'strSourceFile 原始文件完整名
     'strTargetFile 生成新文件的完整名
     'intRow 操作的行數(shù)
     Sub 操作文件中一行(strSourceFile As String, strTargetFile As String, intRow As Long)
     Dim filenum As Integer
     Dim fileContents As String
     Dim fileInfo() As String
     Dim i As Integer
     Dim j As Integer
     filenum = FreeFile
     Open strSourceFile For Binary As #filenum
     fileContents = Space(LOF(filenum))
     Get #filenum, , fileContents
     Close filenum
     fileInfo = Split(fileContents, vbCrLf)
     '取出源文件行數(shù),按照回車換行來分隔成數(shù)組
     filenum = FreeFile
     If Dir(strTargetFile, vbNormal) <> "" Then
     Kill strTargetFile
     End If
     Dim Filestr() As String
     '刪除一行代碼塊
     Open strTargetFile For Append As #filenum
     '循環(huán)每一行
     For i = 0 To UBound(fileInfo) - 1
     If i <> intRow - 1 Then
     Print #filenum, fileInfo(i)
     End If
     Next
     Close #filenum
    '替換一行代碼塊
     Open strTargetFile For Append As #filenum
     '循環(huán)每一行
     For i = 0 To UBound(fileInfo) - 1
     If i = intRow - 1 Then
     Print #filenum, "你要替換進(jìn)去的內(nèi)容"
     End If
     Next
     Close #filenum
     '插入一行代碼塊
     Open strTargetFile For Append As #filenum
     '循環(huán)每一行
     For i = 0 To UBound(fileInfo) - 1
     If i = intRow - 1 Then
     Print #filenum, "你要插入到這行的內(nèi)容"
     Print #filenum, fileInfo(i) '保留原來的行,位置后移一位
     End If
     Next
     Close #filenum
     MsgBox "完畢"
     End Sub
     '另外一個(gè)解決實(shí)際問題的例子
     '
     '網(wǎng)友的要求
     '設(shè)有文件a.txt,其中存放了兩行數(shù)據(jù),數(shù)據(jù)用逗號分隔,現(xiàn)在要讀取第一行的奇數(shù)位置的數(shù)據(jù)寫 入到另一個(gè)文本文件(b.txt)的第一行,類似地,把第二行的奇數(shù)位置的數(shù)據(jù)寫入到第二行。
     '比如:
     '文件a.txt如下:
     '1,2,3,4,5
     '6,7,8,9,10
     '操作完成后,文件b.txt應(yīng)為
     '1,3,5
     '6,8,10
     '作者: soho_andy (冰)
     '參數(shù):
     'strSourceFile 原始文件完整名
     'strTargetFile 生成新文件的完整名
    Sub 提取奇數(shù)位數(shù)據(jù)(strSourceFile As String, strTargetFile As String)
     Dim filenum As Integer
     Dim fileContents As String
     Dim fileInfo() As String
     Dim i As Integer
     Dim j As Integer
     Dim tmpDemData As String
     filenum = FreeFile
     Open strSourceFile For Binary As #filenum
     fileContents = Space(LOF(filenum))
     Get #filenum, , fileContents
     Close filenum
     fileInfo = Split(fileContents, vbCrLf)
     '取出源文件行數(shù),按照回車換行來分隔成數(shù)組
     filenum = FreeFile
     tmpDemData = ""
     If Dir(strTargetFile, vbNormal) <> "" Then
     Kill strTargetFile End If
     Dim Filestr() As String
     Open strTargetFile For Append As #filenum
     '循環(huán)每一行
     For i = 0 To UBound(fileInfo) - 1
     Filestr = Split(Trim(fileInfo(i)), ",") '按照逗號分隔每一行的數(shù)據(jù)
     tmpDemData = ""
     For j = 0 To UBound(Filestr)
     '判斷是否為奇數(shù)位
     If (j Mod 2) = 0 Then
     tmpDemData = tmpDemData & Filestr(j)
     ElseIf j <> 0 And j <> UBound(Filestr) Then
     tmpDemData = tmpDemData & ","
     End If
     Next
     '保存一行如目標(biāo)文件
     Print #filenum, tmpDemData
     Next
     Close #filenum
     MsgBox "完畢"
     End Sub
     Private Sub Command1_Click()
     提取奇數(shù)位數(shù)據(jù) "d:\aa.txt", "d:\bb.txt"
     End Sub