Using sr2
As
New
StreamWriter(
"2.csv"
,
False
, Text.Encoding.
Default
)
'要写入的文件
Using sr1
As
New
StreamReader(
"1.csv"
, Text.Encoding.
Default
)
'要读取的文件
While
Not
sr1.EndOfStream
'EndOfStream=True表示读取结束了
'读取
Dim
lineread
As
String
=
""
'等下要读的行
Dim
linewrite
As
String
=
""
'等下要写入的行
Dim
data
As
String
()
'每个数据的数组
lineread = sr1.ReadLine
'读一行并把流的位置往后调一行
'你现在可以用If判断这一行要不要删除。如果要的话,用Else直接跳过下面的语句即可。
data = lineread.Split(
","
.ToCharArray, StringSplitOptions.RemoveEmptyEntries)
'读取一行,用逗号分隔后存在数组里
'下面对读取到的数据进行处理,你可以自己处理它
For
Each
item
As
String
In
data
linewrite &= item &
","
'csv是以逗号分隔的,我们写进去时也要记得加逗号
Next
'写入
If
linewrite.EndsWith(
","
)
Then
'去掉行最后一个逗号。
'如果之前读到一个空行,这里就不会执行。你想想为什么
sr2.WriteLine(linewrite.Remove(linewrite.Length - 1, 1))
End
If
'如果你之前用If判断了行要不要删除,那么End If就应该加在这里。
End
While
sr1.Close()
End
Using
sr2.Flush()
sr2.Close()
End
Using
---------------------------------------------------------------------------------------------------------------------------------------------------------------
Imports System
Imports System.IO
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Try
If File.Exists(path) Then
File.Delete(path)
End If
Dim sw As StreamWriter = New StreamWriter(path)
sw.WriteLine( "This")
sw.WriteLine( "is some text")
sw.WriteLine( "to test")
sw.WriteLine( "Reading")
sw.Close()
Dim sr As StreamReader = New StreamReader(path)
Do While sr.Peek() > -1
Console.WriteLine(sr.ReadLine())
Loop
sr.Close()
Catch e As Exception
Console.WriteLine( "The process failed: {0}", e.ToString())
End Try
End Sub
End Class
Imports System.Text
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Try
If File.Exists(path) Then
File.Delete(path)
End If
Dim sw As StreamWriter = New StreamWriter(path)
sw.WriteLine( "This")
sw.WriteLine( "is some text")
sw.WriteLine( "to test")
sw.WriteLine( "Reading")
sw.Close()
Dim sr As StreamReader = New StreamReader(path)
Do While sr.Peek() > -1
Console.WriteLine(sr.ReadLine())
Loop
sr.Close()
Catch e As Exception
Console.WriteLine( "The process failed: {0}", e.ToString())
End Try
End Sub
End Class