新聞中心
vb.net 將文件轉(zhuǎn)化成二進(jìn)制
首先引入System.IO命名空間
創(chuàng)新互聯(lián)建站專注于楚雄州網(wǎng)站建設(shè)服務(wù)及定制,我們擁有豐富的企業(yè)做網(wǎng)站經(jīng)驗(yàn)。 熱誠(chéng)為您提供楚雄州營(yíng)銷型網(wǎng)站建設(shè),楚雄州網(wǎng)站制作、楚雄州網(wǎng)頁(yè)設(shè)計(jì)、楚雄州網(wǎng)站官網(wǎng)定制、微信平臺(tái)小程序開發(fā)服務(wù),打造楚雄州網(wǎng)絡(luò)公司原創(chuàng)品牌,更為您提供楚雄州網(wǎng)站排名全網(wǎng)營(yíng)銷落地服務(wù)。
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就是整個(gè)文件的所有字節(jié)了
從字節(jié)生成Image:
Dim img As Image = Image.FromStream(New MemoryStream(bytes))
img就是圖片了
vb.net讀取文件流
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
VB.NET 怎么讀寫二進(jìn)制文件,類似Open
本示例闡釋二進(jìn)制文件的基本輸入和輸出(使用 BinaryReader、BinaryWriter 和 FileStream 類。 在如何創(chuàng)建日志文件標(biāo)題下面有一個(gè)類似的主題。讀寫二進(jìn)制信息使您可以創(chuàng)建和使用通過其他輸入和輸出方法無(wú)法訪問的文件。本示例還展示寫入非字符串?dāng)?shù)據(jù),并展示二進(jìn)制 I/O 的功能。
盡管計(jì)算機(jī)上的文件可以不同的類型和文件存儲(chǔ),但是,二進(jìn)制格式是文件的較常用格式之一。此處對(duì)創(chuàng)建二進(jìn)制文件的簡(jiǎn)短介紹使用基類 BinaryReader 和 BinaryWriter 從文件獲取信息,并將信息放入文件。這些類中的每個(gè)類均封裝一個(gè)信息流,因此,在進(jìn)一步操作之前,需要?jiǎng)?chuàng)建一個(gè)可用于來回寫信息的流。因?yàn)橐獎(jiǎng)?chuàng)建文件,所以可使用 FileStream 來公開特定文件,在此情況下,如果該文件已存在,則可以修改該文件,或者如果該文件尚不存在,則可以創(chuàng)建該文件。在有 FileStream 之后,可以使用它來構(gòu)造 BinaryReader 和 BinaryWriter
在讀入信息之后,可以對(duì)信息進(jìn)行所需的任何操作。但是,在某些時(shí)候,您可能想要將信息寫回文件,因此需要 BinaryWriter。在本示例中,您將使用 Seek 方法將信息追加到文件結(jié)尾,因此,在開始寫入之前,請(qǐng)確保指向文件的指針位于文件結(jié)尾。在使用 BinaryWriter 寫入信息時(shí)有多個(gè)選項(xiàng)。因?yàn)?Write 方法有足夠的重載用于您能夠?qū)懭氲乃行畔㈩愋停裕梢允褂?Write 方法向您的編寫器封裝的流寫入任何標(biāo)準(zhǔn)形式的信息。本情況下,還可以使用 WriteString 方法向流中寫入長(zhǎng)度預(yù)先固定的字符串。
VB Source: VB\ReadWrite.aspx
%@ Import Namespace="System.Text" %
%@ Import Namespace="System.IO" %
script language="VB" runat=server
Class TestBinary
Public Shared Function ReadFile(selection As String) As String
Dim output As StringBuilder = New StringBuilder()
Dim fs As FileStream = New FileStream("data.bin", FileMode.OpenOrCreate)
Dim r As BinaryReader = New BinaryReader(fs)
Try
r.BaseStream.Seek(0,SeekOrigin.Begin) ' 將文件指針設(shè)置到文件開始
' 因?yàn)椴煌瑪?shù)據(jù)類型之間的很多轉(zhuǎn)換結(jié)果都是不可解釋的,
' 所以當(dāng)在其他類型與二進(jìn)制數(shù)據(jù)之間進(jìn)行轉(zhuǎn)換時(shí),
' 必須捕捉可能引發(fā)的任何潛在的異常...
' 能夠正確讀取數(shù)據(jù)依賴于如何寫入信息...
' 這與寫日志文件時(shí)不同。
Do While r.BaseStream.Position r.BaseStream.Length ' 當(dāng)未到達(dá)文件結(jié)尾時(shí)
Select Case selection
Case "Boolean"
output.Append( r.ReadBoolean().ToString() )
Case "String"
output.Append( r.ReadString() )
Case "Integer"
output.Append( r.ReadInt32().ToString() )
End Select
Loop
Finally
fs.Close()
End Try
return output.ToString()
End Function
Public Shared Function WriteFile(output As Object, selection As String) As String
Dim fs As FileStream = New FileStream("data.bin", FileMode.Create)
Dim w As BinaryWriter = New BinaryWriter(fs)
Dim strOutput As String = ""
w.BaseStream.Seek(0, SeekOrigin.End) ' 將文件指針設(shè)置到文件結(jié)尾
' 因?yàn)檎趯懙男畔⒖赡懿贿m合于所選擇用于寫入的特定樣式
' (例如,單詞“Hello”作為整數(shù)?),所以我們必須捕捉寫入
' 錯(cuò)誤,并通知用戶未能執(zhí)行該任務(wù)
Try
Select Case selection
Case "Boolean"
Dim b As Boolean = Convert.ToBoolean(output)
w.Write( b )
Case "String"
Dim s As String = Convert.ToString(output)
w.Write( s )
Case "Integer"
Dim i As Int32 = Convert.ToInt32(output)
w.Write(i)
End Select
Catch E As Exception
' 讓用戶知道未能寫入該信息
strOutput = "寫異常:" chr(13) _
"無(wú)法以所請(qǐng)求的格式寫入要寫入的信息。" _
chr(13) "請(qǐng)輸入嘗試寫入的數(shù)據(jù)類型的有效值"
End Try
fs.Close()
return strOutput
End Function
End Class
Sub btnAction_Click(src As Object, E As EventArgs)
Dim s As String = ""
' 寫出文件
s = TestBinary.WriteFile(txtInput.Text, lstDataIn.SelectedItem.Text)
If s = "" Then
Try
' 讀回信息,顯示信息...
txtOutput.Text = TestBinary.ReadFile(lstDataIn.SelectedItem.Text)
Catch Exc As Exception
' 讓用戶知道未能寫入信息
s = "讀異常:" chr(13) _
"無(wú)法以所請(qǐng)求的格式讀取要寫入的信息。" _
chr(13) "請(qǐng)輸入嘗試寫入的數(shù)據(jù)類型的有效值"
End Try
Else
txtOutput.Text = s
End If
End Sub
/script
html
head
link rel="stylesheet" href="intro.css"
/head
body style="background-color:f6e4c6"
form method=post runat="server"
p
table
tr
tdb
下面的示例使用 BinaryWriter 對(duì)象創(chuàng)建一個(gè)二進(jìn)制文件,然后使用 BinaryReader 讀取該信息。/b可以選擇不同的對(duì)象來將所需的信息寫入文件
此演示用于強(qiáng)調(diào)您需要知道如何讀取已寫入的二進(jìn)制文件。一旦以某種格式寫入數(shù)據(jù),就只能以該格式讀取該信息。但是,可以將多種不同的數(shù)據(jù)類型寫入文件。在此演示中,輸入任意字符串并將它們作為字符串讀取,對(duì)于整型,僅輸入整型數(shù)值項(xiàng)(試試浮點(diǎn)數(shù)字,然后看看會(huì)發(fā)生什么...);對(duì)于布爾型項(xiàng),僅輸入詞“false”和“true”。
p
hr
/td
/tr
/table
asp:Table id="basetable" runat="server" border="0" cellspacing="0" cellpadding="5"
asp:tablerow
asp:tablecell verticalalign="top"
請(qǐng)選擇要保存到二進(jìn)制文件的數(shù)據(jù)類型...
/asp:tablecell
asp:tablecell verticalalign="top"
asp:listbox id="lstDataIn" runat="server"
asp:listitemBoolean/asp:listitem
asp:listitem selected="true"String/asp:listitem
asp:listitemInteger/asp:listitem
/asp:listbox
/asp:tablecell
asp:tablecell verticalalign="top"
asp:button id="btnAction" onclick="btnAction_Click" Text="寫入/讀取文件" runat="server"/
/asp:tablecell
/asp:tablerow
2010VB.net 如何一直讀取TXT文檔最后一行信息,麻煩給個(gè)代碼,謝謝
首先在代碼的開頭引入命名空間:
Imports?System.IO
給出三個(gè)讀文本文件的例子。
'直接讀取所有文本
Dim?fileString?As?String?=?File.ReadAllText("1.txt")
'把所有行弄進(jìn)一個(gè)數(shù)組
Dim?fileLines()?As?String?=?File.ReadAllLines("1.txt")
'以流的方式一行一行讀取
Using?fs?As?FileStream?=?File.OpenRead("1.txt")?'初始化文件流
Using?sr?As?New?StreamReader(fs)?'初始化流讀取器
Dim?lineString?As?String?=?""
While?Not?sr.EndOfStream?'循環(huán)直到流結(jié)束
lineString?=?sr.ReadLine()?'讀一行
'這里lineString就是每行的文件內(nèi)容了。
'你可以在這里對(duì)它進(jìn)行處理。
End?While
End?Using
End?Using
vb.net使用打開對(duì)話框,打開一個(gè)文本文件顯示在文本框中,然后對(duì)文本框中的信息進(jìn)行編輯后
讀文件內(nèi)容到TextBox :
Try
Using sr As StreamReader = New StreamReader("C:\\TestFile.txt")
textbox1.Text = sr.ReadToEnd()
End Using
Catch E As Exception
Console.WriteLine(E.Message)
End Try
修改完成后,保存到文件:
Using sw As StreamWriter = New StreamWriter("C:\\TestFile.txt")
sw.Write(textbox1.Text)
sw.Close()
End Using
其中,流構(gòu)造的參數(shù)
New StreamReader("C:\\TestFile.txt")
New StreamWriter("C:\\TestFile.txt")
中的"C:\\TestFile.txt"可以用OpenFileDialog和SaveFileDialog的FileName屬性替換.
Dim openFileDialog1 As New OpenFileDialog()
openFileDialog1.InitialDirectory = "c:\"
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Try
Using sr As StreamReader = New StreamReader(openFileDialog1.FileName)
textbox1.Text = sr.ReadToEnd()
End Using
Catch E As Exception
Console.WriteLine(E.Message)
End Try
End If
保存的時(shí)候換成SaveFileDialog就好
名稱欄目:關(guān)于vb.net文件流的信息
轉(zhuǎn)載來源:http://www.ef60e0e.cn/article/hpdpdg.html