vb.net中讀寫文件 vbs 讀寫文件

請教在VB.net中如何將數(shù)據(jù)寫入txt文件、再從txt文件讀出?

軟糖來告訴你吧。

10年積累的成都做網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)經(jīng)驗,可以快速應對客戶對網(wǎng)站的新想法和需求。提供各種問題對應的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認識你,你也不認識我。但先網(wǎng)站設(shè)計后付款的網(wǎng)站建設(shè)流程,更有南豐免費網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。

VB.net中讀寫文件主要使用System.IO命名空間。

① 使用 File.ReadAllText 讀取

Dim s As String = System.IO.File.ReadAllText("C:\a.txt")

② 使用 StreamReader 讀取,注意編碼格式和寫入的編碼保持一致。

Dim sr As StreamReader = New StreamReader("C:\a.txt", System.Text.Encoding.UTF8)

Dim s As String = sr.ReadToEnd()

sr.Close()

③ 使用 File.WriteAllText 寫入,會覆蓋同名的文件。

Dim 要寫的內(nèi)容 As String = ""

File.WriteAllText(文件路徑, 要寫的內(nèi)容, System.Text.Encoding.UTF8)

④ 使用 StreamWriter 寫入。

Dim sw As System.IO.StreamWriter = New System.IO.StreamWriter("C:\a.txt", False, System.Text.Encoding.UTF8)

sw.WriteLine(TextTB.Text)

sw.Close()

⑤ 使用 StreamWriter 追加寫入。

將上面代碼的第二個參數(shù)False改為True。

◆ 滿意請采納,謝謝 ◆

VB.NET 怎么讀寫二進制文件,類似Open

本示例闡釋二進制文件的基本輸入和輸出(使用 BinaryReader、BinaryWriter 和 FileStream 類。 在如何創(chuàng)建日志文件標題下面有一個類似的主題。讀寫二進制信息使您可以創(chuàng)建和使用通過其他輸入和輸出方法無法訪問的文件。本示例還展示寫入非字符串數(shù)據(jù),并展示二進制 I/O 的功能。

盡管計算機上的文件可以不同的類型和文件存儲,但是,二進制格式是文件的較常用格式之一。此處對創(chuàng)建二進制文件的簡短介紹使用基類 BinaryReader 和 BinaryWriter 從文件獲取信息,并將信息放入文件。這些類中的每個類均封裝一個信息流,因此,在進一步操作之前,需要創(chuàng)建一個可用于來回寫信息的流。因為要創(chuàng)建文件,所以可使用 FileStream 來公開特定文件,在此情況下,如果該文件已存在,則可以修改該文件,或者如果該文件尚不存在,則可以創(chuàng)建該文件。在有 FileStream 之后,可以使用它來構(gòu)造 BinaryReader 和 BinaryWriter

在讀入信息之后,可以對信息進行所需的任何操作。但是,在某些時候,您可能想要將信息寫回文件,因此需要 BinaryWriter。在本示例中,您將使用 Seek 方法將信息追加到文件結(jié)尾,因此,在開始寫入之前,請確保指向文件的指針位于文件結(jié)尾。在使用 BinaryWriter 寫入信息時有多個選項。因為 Write 方法有足夠的重載用于您能夠?qū)懭氲乃行畔㈩愋?,所以,可以使?Write 方法向您的編寫器封裝的流寫入任何標準形式的信息。本情況下,還可以使用 WriteString 方法向流中寫入長度預先固定的字符串。

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è)置到文件開始

' 因為不同數(shù)據(jù)類型之間的很多轉(zhuǎn)換結(jié)果都是不可解釋的,

' 所以當在其他類型與二進制數(shù)據(jù)之間進行轉(zhuǎn)換時,

' 必須捕捉可能引發(fā)的任何潛在的異常...

' 能夠正確讀取數(shù)據(jù)依賴于如何寫入信息...

' 這與寫日志文件時不同。

Do While r.BaseStream.Position r.BaseStream.Length ' 當未到達文件結(jié)尾時

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é)尾

' 因為正在寫的信息可能不適合于所選擇用于寫入的特定樣式

' (例如,單詞“Hello”作為整數(shù)?),所以我們必須捕捉寫入

' 錯誤,并通知用戶未能執(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) _

"無法以所請求的格式寫入要寫入的信息。" _

chr(13) "請輸入嘗試寫入的數(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) _

"無法以所請求的格式讀取要寫入的信息。" _

chr(13) "請輸入嘗試寫入的數(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 對象創(chuàng)建一個二進制文件,然后使用 BinaryReader 讀取該信息。/b可以選擇不同的對象來將所需的信息寫入文件

此演示用于強調(diào)您需要知道如何讀取已寫入的二進制文件。一旦以某種格式寫入數(shù)據(jù),就只能以該格式讀取該信息。但是,可以將多種不同的數(shù)據(jù)類型寫入文件。在此演示中,輸入任意字符串并將它們作為字符串讀取,對于整型,僅輸入整型數(shù)值項(試試浮點數(shù)字,然后看看會發(fā)生什么...);對于布爾型項,僅輸入詞“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"

請選擇要保存到二進制文件的數(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

vb.net中,讀取和寫入文件

寫入:Dim sr As New IO.StreamWriter(Application.StartupPath "/寫入的文本.txt")

sr.WriteLine("寫入的內(nèi)容") sr.Close()讀取:If (File.Exists(Application.StartupPath "/msg.txt")) Then

Dim fm As New IO.FileStream(Application.StartupPath "/讀取的文本.txt", FileMode.Open)

Dim sr As IO.StreamReader = New IO.StreamReader(fm)

Do While sr.Peek() = 0

TextBox1.Text = sr.ReadLine() (讀取文本到文本框)

Loop end if

vb.net 中如何進行文件的讀寫

一般來講asp.net的服務(wù)器運行應用所用的賬號只有對應用所在目錄(放網(wǎng)頁的目錄)的訪問權(quán)限,你可以把文件存儲在應用所在目錄或者子目錄試試。

My.Computer.FileSystem.WriteAllText(Server.MapPath("") “\" Now.toString("yyyyMMdd") ".txt", "URL:XXXXXX查詢完成,匹配成功!" vbCrLf, true, System.Text.Encoding.Default)

當前文章:vb.net中讀寫文件 vbs 讀寫文件
路徑分享:http://bm7419.com/article46/dohhghg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、建站公司、網(wǎng)站改版、定制網(wǎng)站、網(wǎng)站內(nèi)鏈、ChatGPT

廣告

聲明:本網(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)

h5響應式網(wǎng)站建設(shè)