vb.net 如何从visual basic中的文本文件中删除第一行和最后一行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2099505/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete the first and last lines from a text file in visual basic
提问by Logan Young
I've seen posts on deleting the lines from a text file that are specified as a function parameter, but I only need to delete the first and last lines from the file.
我看过有关从文本文件中删除指定为函数参数的行的帖子,但我只需要从文件中删除第一行和最后一行。
I'm still a newbie when it comes to working in files, but it seems that it should be simple to delete the first line... Just delete all text from BOF to the first CrLf character. Am I right?
在处理文件方面,我仍然是一个新手,但删除第一行似乎应该很简单......只需删除 BOF 到第一个 CrLf 字符的所有文本。我对吗?
As for the last line, I understand that I'll have to get a count of the lines in the text file to find it (as the file won't always be x amount of lines long). This is where I really need some help.
至于最后一行,我知道我必须计算文本文件中的行数才能找到它(因为文件并不总是 x 行长)。这是我真正需要帮助的地方。
N.B. I'm using VB.NET 2005
注意我使用的是 VB.NET 2005
回答by Joel Etherton
Read the file in to a list of lines as string completely. Write the file back out using an indexed loop to capture all but the first and last items.
将文件完全作为字符串读入行列表。使用索引循环将文件写回以捕获除第一个和最后一个项目之外的所有项目。
Dim listText As New List(Of String)
Dim objLine As String = ""
Using objReader As StreamReader = New StreamReader("c:\test.txt")
Do
objLine = objReader.ReadLine()
If objLine IsNot Nothing Then listText.Add(objLine)
Loop Until objLine Is Nothing
End Using
Using objWriter As StreamWriter = New StreamWriter("c:\testOutput.txt")
For I As Integer = 1 To listText.Count - 2
objWriter.WriteLine(listText.Item(I))
Next
End Using
Edit to satisfy the very picky:
编辑以满足非常挑剔的:
Dim arrText() As String
Dim sLine As String = ""
arrText = File.ReadAllLines("c:\test.txt")
Using objWriter As StreamWriter = New StreamWriter("c:\testOutput.txt")
For I As Integer = 1 To arrText.Length - 2
objWriter.WriteLine(arrText(I))
Next
End Using
回答by Hans Passant
Files are streams, there are no shortcuts. You have to read the entire file and write it back, minus the first and last line. Horribly inefficient of course, use a database instead.
文件是流,没有快捷方式。您必须读取整个文件并将其写回,减去第一行和最后一行。当然,效率极低,请改用数据库。
回答by dbasnett
This is to delete the last line, without reading the entire file. You may need to modify the logic to account for the EOF being a newline...
这是删除最后一行,而不读取整个文件。您可能需要修改逻辑以将 EOF 作为换行符...
Dim fs As New FileStream("c:\test.txt", FileMode.Open, FileAccess.ReadWrite)
Dim b(1) As Byte
Do
fs.Seek(fs.Length - 2, SeekOrigin.Begin) 'seek 2 bytes from EOF
fs.Read(b, 0, 2) 'read last two bytes
'are they newline
If System.Text.Encoding.ASCII.GetString(b, 0, 2) <> Environment.NewLine Then
fs.SetLength(fs.Length - 1) 'set length to -1
Else
fs.SetLength(fs.Length - 2)
Exit Do
End If
Loop
fs.Close()