展开全部
现在又有人提出这个问题了.. 针对这样的问题,我特意写了一个删除txt文本文件指定行数的函数,如果想删除某一行直接调用这个函数就行了.我给你把源代码(+非常详细的注释)给你发出来吧.. 希望你能给我追加点分数啊...o(∩_∩)o...
Private Function DelLine(strFile As String, RLine As Long, newFile As String, SameLine As Boolean)
Dim s As String, n As String, i As Long
i = 1
'//打开源文件
Open strFile For Input As #1
Do Until EOF(1)
Line Input #1, s
If RLine = i Then '如果是指定的行数就进行下面的操作
'------------------------------------------------------
If SameLine = True Then '是否保持源文件行数不变(以空白字符替换这一行内容)的提示,True保持源文件的行数,False为直接删除这一行的内容
s = ""
n = n & s & vbCrLf '将空字符串赋给变量n,以保持源文件的行数
' MsgBox strFile & " 文件中,第 " & RLine & " 行内容" & vbCrLf & s & vbCrLf & "已经删除", vbInformation, "消息提示"
End If
' s="也可以把这一行的内容改成自己需要的"
'-------------------------------------------------------
Else '如果不是指定的行数,就将s的内容赋给变量n 以存储数据
n = n & s & vbCrLf '将s的内容赋给n 并以一个回车符号结束....
End If
i = i + 1
Loop
Close #1
'//写入新文件,如果和源文件同名则会覆盖源文件
Open newFile For Output As #2
Print #2, n '将n变量里的数据写入新文件
Close #2
End Function
'调用方法:
'比如要把c:\1.txt 删除其中的第5行内容,并保留源文件总行数(删除的这行插入一空字符串)
DelLine "C:\1.txt", 5, "C:\2.txt", True
'删除C:\1.txt 删除里面的第一行,且不保留文件的总行数
DelLine "C:\1.txt", 1, "C:\1.txt", False
Private Function DelLine(strFile As String, RLine As Long, newFile As String, SameLine As Boolean)
Dim s As String, n As String, i As Long
i = 1
'//打开源文件
Open strFile For Input As #1
Do Until EOF(1)
Line Input #1, s
If RLine = i Then '如果是指定的行数就进行下面的操作
'------------------------------------------------------
If SameLine = True Then '是否保持源文件行数不变(以空白字符替换这一行内容)的提示,True保持源文件的行数,False为直接删除这一行的内容
s = ""
n = n & s & vbCrLf '将空字符串赋给变量n,以保持源文件的行数
' MsgBox strFile & " 文件中,第 " & RLine & " 行内容" & vbCrLf & s & vbCrLf & "已经删除", vbInformation, "消息提示"
End If
' s="也可以把这一行的内容改成自己需要的"
'-------------------------------------------------------
Else '如果不是指定的行数,就将s的内容赋给变量n 以存储数据
n = n & s & vbCrLf '将s的内容赋给n 并以一个回车符号结束....
End If
i = i + 1
Loop
Close #1
'//写入新文件,如果和源文件同名则会覆盖源文件
Open newFile For Output As #2
Print #2, n '将n变量里的数据写入新文件
Close #2
End Function
'调用方法:
'比如要把c:\1.txt 删除其中的第5行内容,并保留源文件总行数(删除的这行插入一空字符串)
DelLine "C:\1.txt", 5, "C:\2.txt", True
'删除C:\1.txt 删除里面的第一行,且不保留文件的总行数
DelLine "C:\1.txt", 1, "C:\1.txt", False
展开全部
http://bbs.csdn.net/topics/390090827?page=1#post-393895940
我只是一个搬运工,让大家都看到吧
Option Explicit
Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
Dim st1 As Long, st2 As Long, et1 As Long, et2 As Long
Private Sub Command1_Click()
Dim FileNumber As Integer, URL As String, i As Long
Dim FileByte() As Byte, sFile() As String
FileNumber = FreeFile
Open "c:\wangzhi.txt" For Binary As #FileNumber
Line Input #FileNumber, URL '取得文件第一行
Close #FileNumber
Debug.Print "url=" & URL
i = LenB(StrConv(URL & vbCrLf, vbFromUnicode)) '第一行字节数
'获取第一行以后的其他行,并保存到字节数组FileByte中
st1 = GetTickCount
FileNumber = FreeFile
Open "c:\wangzhi.txt" For Binary As #FileNumber
Erase FileByte
ReDim FileByte(LOF(FileNumber) - 1 - i)
Get #FileNumber, i + 1, FileByte
Close #FileNumber
et1 = GetTickCount
st2 = GetTickCount
'将其他行写入文件中
FileNumber = FreeFile
Open "c:\wangzhi1.txt" For Binary As #FileNumber
'For i = 1 To UBound(sFile)
Put #FileNumber, , FileByte
'Next
Close #FileNumber
et2 = GetTickCount
Debug.Print "读出内容时间:" & (et1 - st1) / 1000 & "秒", "写入内容时间:" & (et2 - st2) / 1000 & "秒"
End Sub
我只是一个搬运工,让大家都看到吧
Option Explicit
Private Declare Function GetTickCount Lib "kernel32.dll" () As Long
Dim st1 As Long, st2 As Long, et1 As Long, et2 As Long
Private Sub Command1_Click()
Dim FileNumber As Integer, URL As String, i As Long
Dim FileByte() As Byte, sFile() As String
FileNumber = FreeFile
Open "c:\wangzhi.txt" For Binary As #FileNumber
Line Input #FileNumber, URL '取得文件第一行
Close #FileNumber
Debug.Print "url=" & URL
i = LenB(StrConv(URL & vbCrLf, vbFromUnicode)) '第一行字节数
'获取第一行以后的其他行,并保存到字节数组FileByte中
st1 = GetTickCount
FileNumber = FreeFile
Open "c:\wangzhi.txt" For Binary As #FileNumber
Erase FileByte
ReDim FileByte(LOF(FileNumber) - 1 - i)
Get #FileNumber, i + 1, FileByte
Close #FileNumber
et1 = GetTickCount
st2 = GetTickCount
'将其他行写入文件中
FileNumber = FreeFile
Open "c:\wangzhi1.txt" For Binary As #FileNumber
'For i = 1 To UBound(sFile)
Put #FileNumber, , FileByte
'Next
Close #FileNumber
et2 = GetTickCount
Debug.Print "读出内容时间:" & (et1 - st1) / 1000 & "秒", "写入内容时间:" & (et2 - st2) / 1000 & "秒"
End Sub
参考资料: http://bbs.csdn.net/topics/390090827?page=1#post-393895940
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询