重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
我给你一个实例,你自己修改;
创新互联公司主要从事成都网站设计、成都网站制作、网页设计、企业做网站、公司建网站等业务。立足成都服务金口河,10余年网站建设经验,价格优惠、服务专业,欢迎来电咨询建站服务: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读取网络流时需要为他分配一定的系统资源,调用他的close()函数就是关闭这个读取器并释放系统分配给他的资源。
System.IO.StreamReader objread = new System.IO.StreamReader(path);
System.IO.Stream stream = objread.BaseStream;
objread.Close();
所有信息全在stream 里了
无法直接获取,但可以采用变通的方法,思路:
把word转换成二进制流前先用变量把拓展名和后缀获取到(例如 var filename = “xxxx.doc”)
定义一个int变量记录二进制流(word)的长度。并将该变量转成4字节的btye[]数组
将第一步中获取到的文件名字符串转成byte[]数组。
将字节按照: word文件byte[]+文件名byte[]+word文件长度byte[](第二步)按照顺序拼接成一个byte[]数组
还原:
1.首先读取总byte[]的后4个字节,以确定文件二进制流的有效长度(假设为L).
2.将索引0至L 之间的字节数组按常规方式恢复成流.
3.将索引L至N-4之间的字节数组还原成字符串,即可获得原文件名.
然后,爱咋咋地~~~~
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是你要的结果,为byte(), End Using