我給你一個實例,你自己修改;
創(chuàng)新互聯(lián)公司主要從事成都網(wǎng)站設(shè)計、成都網(wǎng)站制作、網(wǎng)頁設(shè)計、企業(yè)做網(wǎng)站、公司建網(wǎng)站等業(yè)務(wù)。立足成都服務(wù)金口河,10余年網(wǎng)站建設(shè)經(jīng)驗,價格優(yōu)惠、服務(wù)專業(yè),歡迎來電咨詢建站服務(wù):18980820575
一,運行界面:
二,完整代碼:
Imports System.IO
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'寫文件
Dim Myw As New FileStream(Application.StartupPath "\實驗文件.txt", FileMode.Create)
Dim MyB_Write As BinaryWriter = New BinaryWriter(Myw)
MyB_Write.Write(TextBox1.Text)
Myw.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'讀文件
Dim Myr As New FileStream(Application.StartupPath "\實驗文件.txt", FileMode.Open, FileAccess.Read)
Myr.Position = 0
Dim MyB_Read As New BinaryReader(Myr)
Dim MyFileLength As Integer = CInt(Myr.Length - Myr.Position) - 1
Dim MyFileData(MyFileLength) As Char
MyB_Read.Read(MyFileData, 0, MyFileLength)
Myr.Close()
TextBox2.Text = ""
Dim i As Integer
For i = LBound(MyFileData) To UBound(MyFileData)
TextBox2.Text = TextBox2.Text MyFileData(i)
Next
End Sub
End Class
StreamReader讀取網(wǎng)絡(luò)流時需要為他分配一定的系統(tǒng)資源,調(diào)用他的close()函數(shù)就是關(guān)閉這個讀取器并釋放系統(tǒng)分配給他的資源。
System.IO.StreamReader objread = new System.IO.StreamReader(path);
System.IO.Stream stream = objread.BaseStream;
objread.Close();
所有信息全在stream 里了
無法直接獲取,但可以采用變通的方法,思路:
把word轉(zhuǎn)換成二進制流前先用變量把拓展名和后綴獲取到(例如 var filename = “xxxx.doc”)
定義一個int變量記錄二進制流(word)的長度。并將該變量轉(zhuǎn)成4字節(jié)的btye[]數(shù)組
將第一步中獲取到的文件名字符串轉(zhuǎn)成byte[]數(shù)組。
將字節(jié)按照: word文件byte[]+文件名byte[]+word文件長度byte[](第二步)按照順序拼接成一個byte[]數(shù)組
還原:
1.首先讀取總byte[]的后4個字節(jié),以確定文件二進制流的有效長度(假設(shè)為L).
2.將索引0至L 之間的字節(jié)數(shù)組按常規(guī)方式恢復(fù)成流.
3.將索引L至N-4之間的字節(jié)數(shù)組還原成字符串,即可獲得原文件名.
然后,愛咋咋地~~~~
dim filename as string = "文件名" Using myfilestream As New FileStream(FileName, FileMode.Open, FileAccess.Read)
Dim data() As Byte
ReDim data(myfilestream.Length - 1)
myfilestream.Read(data, 0, myfilestream.Length)
myfilestream.Close()
' data是你要的結(jié)果,為byte(), End Using
當(dāng)前文章:vb.net中的流的簡單介紹
文章位置:http://chinadenli.net/article26/doddhjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供Google、網(wǎng)站改版、響應(yīng)式網(wǎng)站、企業(yè)建站、品牌網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)