关于VB2005中文件读写流的问题
请问有没有可以方便的对文件里的内容进行操作的方法?
比如说我用Streamwriter对象的writeline方法写入多行文字后,现在要按我的需求对写入的内容进行操作:实现按字符内容判断该删除哪行,或者修改哪行的内容,或插入到哪行?
[解决办法]
修改可以做到,定位后直接覆盖,但删除部分内容不可以实现,你只能重新建一个文件,再把需要保留的内容复制过去。添加可以定位到文件的结尾,直接写数据。
[解决办法]
你在for 循环里这一开一关的,效益不太好!
....
Dim fsw As New StreamWriter(Application.StartupPath & "\default.xhs ", True)
try
For Each ctrl In container.Controls
If TypeOf (ctrl) Is Label = True Then
ObjName = ctrl.Name.ToString
Content = ctrl.Text.ToString
Visible = ctrl.Visible
Enabled = ctrl.Enabled
If Enabled = True Then
fsw.WriteLine(ObjName & "; " & Content & "; " & Visible.ToString & "; " & Enabled.ToString & "; " & "1 ")
Else
fsw.WriteLine(ObjName & "; " & Content & "; " & Visible.ToString & "; " & Enabled.ToString & "; " & "0 ")
End If
End If
Next
For Each ctrl In container2.Controls
If TypeOf (ctrl) Is Label = True Then
ObjName = ctrl.Name
Content = ctrl.Text
Visible = ctrl.Visible
Enabled = ctrl.Enabled
LinkPath = GetLinkPath(ObjName)
If Enabled = True Then
fsw.WriteLine(ObjName & "; " & Content & "; " & Visible.ToString & "; " & Enabled.ToString & "; " & "1 " & "; " & LinkPath)
Else
fsw.WriteLine(ObjName & "; " & Content & "; " & Visible.ToString & "; " & Enabled.ToString & "; " & "0 " & "; " & " ")
End If
End If
Next
finally
fsw.Close()
end try
[解决办法]
文件内容做删除不好处理!需要将文件删除再创建一个文件,再写此文件时排除写入要删除的行.
像这样的需求最好用XmlDocument.
[解决办法]
对文件操作删除部分内容应该不可行 除非是xml文档
[解决办法]
学习