首先引入System.IO命名空間
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對這個行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價值的長期合作伙伴,公司提供的服務(wù)項目有:空間域名、雅安服務(wù)器托管、營銷軟件、網(wǎng)站建設(shè)、滄源網(wǎng)站維護、網(wǎng)站推廣。
Imports System.IO
然后使用文件流來讀入數(shù)組:
Dim bytes() As Byte
Using fs As New FileStream(文件路徑,FileMode.Open)
ReDim bytes(fs.Length-1)
fs.Read(bytes,0,fs.Length)
fs.Close()
End Using
這樣bytes就是整個文件的所有字節(jié)了
從字節(jié)生成Image:
Dim img As Image = Image.FromStream(New MemoryStream(bytes))
img就是圖片了
為什么非要存成TXT文件呢?最好的辦法是將你的這些數(shù)據(jù)存放在DataSet對象里,然后用DataSet對象的WriteXML方法寫入文件。下次讀取時,使用該對象的ReadXML方法就可以取回存在文件中的數(shù)據(jù)了。你想增、刪、編輯都可以,很方便的!如果非要生成TXT文件,可以在DataSet對象順序讀取數(shù)據(jù),然后再生成想要的TXT文件。你也用不著去追究IO流的操作了。
System.IO.StreamReader objread = new System.IO.StreamReader(path);
System.IO.Stream stream = objread.BaseStream;
objread.Close();
所有信息全在stream 里了
Imports System
Imports System.IO
Imports System.TextPublic Class Form2 Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim path As String = "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() = 0
' 'This is an arbitrary size for this example.
' Dim c(5) As Char
' sr.Read(c, 0, c.Length)
' 'The output will look odd, because
' 'only five characters are read at a time.
' 'MsgBox(c)
'Loop
'sr.Close()
Catch ex As Exception
Console.WriteLine("The process failed: {0}", ex.ToString())
End Try End Sub '讀取流
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim path As String = "MyTest.txt" Dim reader As StreamReader = New StreamReader(path)
Dim c(5) As Char
reader.Read(c, 0, c.Length)
MsgBox(c)
reader.Close() End Sub
End Class 解讀Read(arrayChar[]()[], Int32, Int32) buffer 類型:arraySystem..::.Char[]()[]
此方法返回時,包含指定的字符數(shù)組,該數(shù)組的 index 和 (index + count - 1) 之間的值由從當(dāng)前源中讀取的字符替換。 index 類型:System..::.Int32
開始寫入的 buffer 的索引。 count 類型:System..::.Int32
最多讀取的字符數(shù)。