2015-06-17 21 views
-1

我知道如何寫入文件,但是,我希望能夠寫入某一行。我有一個很大的csv文件,裏面有很多行。如何寫入VB中的文件中的某一行

我只能寫到最後一行(使用writeline),但我真的想寫50行的第x行。

我該怎麼做?

+1

當你寫到第x行,你想覆蓋它(總行不變)或在現有的行之間插入一條新行(總行數= 1)? – sblandin

+0

謝謝,你能舉一些例子代碼嗎?這將是非常有益的。 – Tankiogames

+0

如果文件中的行大小固定,可以使用隨機訪問文件(搜索它)技術。什麼是「大」?你是單身用戶嗎? – rheitzman

回答

0

我不知道,如果你可以寫在一個文件中的特定行,但如果你需要,你可以寫你的線條列表,然後寫列表保存到文件

'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()) 
相關問題