Vb.net 如何在VB中写入文件中的某一行
我知道如何写入文件,但我希望能够写入某一行。我有一个很大的csv文件,其中有很多行 我只能写到最后一行(使用writeline),但我真的想写到50行中的x行Vb.net 如何在VB中写入文件中的某一行,vb.net,file,file-access,Vb.net,File,File Access,我知道如何写入文件,但我希望能够写入某一行。我有一个很大的csv文件,其中有很多行 我只能写到最后一行(使用writeline),但我真的想写到50行中的x行 如何做到这一点?我不知道是否可以写入文件中的特定行,但如果需要,可以将行写入列表,然后将列表写入文件 'Declare your list Dim lines As New List(Of String) For Each lineToWrite In YourLines If toInsert
如何做到这一点?我不知道是否可以写入文件中的特定行,但如果需要,可以将行写入列表,然后将列表写入文件
'Declare your list
Dim lines As New List(Of String)
For Each lineToWrite In YourLines
If toInsert Then
'In this way you insert a row at the specified index, but this will grow the list not overwrite.
lines.Insert(indexToInsert, lineToWrite)
ElseIf toOverwrite Then
'In this way you overwrite the item at the specified index
lines(indexToOverwrite) = lineToWrite
Else
'Or you can just append it
lines.Add(lineToWrite)
End If
Next
'This will write the file
System.IO.File.WriteAllLines("c:\yourfile.txt", lines.ToArray())
当您写入x行时,您希望覆盖它(总行数不变)或在现有行数之间插入新行(总行数+=1)?谢谢,您能给出一些示例代码吗?如果文件中的行是固定大小的,你可以使用随机访问文件(搜索)技术。“大”是什么?你是单一用户吗?