VB.NET读取文件中的数字并相加或者排序,如何操作,用到哪些类?
VB.NET读取文件中的数字并相加或者排序,如何操作,用到哪些类?如,(Button1)随机生成10个数的文件\Exam.txt,然后(Button2)将10个数读出相加,(Button3)将结果追加至文件,怎么做?下面的程序对否?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim myfile As New FileStream("\Exam.txt", FileMode.OpenOrCreate, FileAccess.Write)
Dim mybwriter As New BinaryWriter(myfile, System.Text.Encoding.Unicode)
Dim i, number As Integer
For i = 0 To 9
number = 10 + Int(90 * Rnd())
mybwriter.Write(number)
Next i
mybwriter.Close()
End Sub
Public Shared sum As Integer
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim myfile As New FileStream("Exam.txt", FileMode.Open, FileAccess.Read)
Dim mybreader As New BinaryReader(myfile,system.Text.Encoding.Unicode)
Dim i, number() As Integer
For i = 0 To 9
number(i) = mybreader.ReadInt32()
sum = sum + number(i)
Next i
mybreader.Close()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Dim myfile As New FileStream("\Exam.txt", FileMode.OpenOrCreate, FileAccess.Write)
Dim mybwriter As New BinaryWriter(myfile, System.Text.Encoding.Unicode)
mybwriter.Write(sum)
mybwriter.Close()
End Sub
mybwriter.Close()
End Sub --------------------编程问答-------------------- 追加文件,可以使用File.AppendAllText方法,如下
--------------------编程问答-------------------- 可以去msdn上搜一下,有关文件读些方面的例子 --------------------编程问答-------------------- mark
System.IO.File.AppendAllText("文件路徑","要追加的字符串", System.Text.Encoding.Unicode)
补充:.NET技术 , VB.NET