首先引入System.IO命名空间
让客户满意是我们工作的目标,不断超越客户的期望值来自于我们对这个行业的热爱。我们立志把好的技术通过有效、简单的方式提供给客户,将通过不懈努力成为客户在信息化领域值得信任、有价值的长期合作伙伴,公司提供的服务项目有:空间域名、雅安服务器托管、营销软件、网站建设、沧源网站维护、网站推广。
Imports System.IO
然后使用文件流来读入数组:
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就是整个文件的所有字节了
从字节生成Image:
Dim img As Image = Image.FromStream(New MemoryStream(bytes))
img就是图片了
为什么非要存成TXT文件呢?最好的办法是将你的这些数据存放在DataSet对象里,然后用DataSet对象的WriteXML方法写入文件。下次读取时,使用该对象的ReadXML方法就可以取回存在文件中的数据了。你想增、删、编辑都可以,很方便的!如果非要生成TXT文件,可以在DataSet对象顺序读取数据,然后再生成想要的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[]()[]
此方法返回时,包含指定的字符数组,该数组的 index 和 (index + count - 1) 之间的值由从当前源中读取的字符替换。 index 类型:System..::.Int32
开始写入的 buffer 的索引。 count 类型:System..::.Int32
最多读取的字符数。