vb.net內(nèi)存數(shù)據(jù)流的簡(jiǎn)單介紹

我用VB.NET編了個(gè)程序,在加在一些數(shù)據(jù)是,會(huì)占用很多系統(tǒng)資源

可以將文件等分成兩部分,分別用兩個(gè)private讀取就是2線(xiàn)程。

在隴川等地區(qū),都構(gòu)建了全面的區(qū)域性戰(zhàn)略布局,加強(qiáng)發(fā)展的系統(tǒng)性、市場(chǎng)前瞻性、產(chǎn)品創(chuàng)新能力,以專(zhuān)注、極致的服務(wù)理念,為客戶(hù)提供做網(wǎng)站、成都網(wǎng)站建設(shè) 網(wǎng)站設(shè)計(jì)制作按需定制,公司網(wǎng)站建設(shè),企業(yè)網(wǎng)站建設(shè),成都品牌網(wǎng)站建設(shè),成都營(yíng)銷(xiāo)網(wǎng)站建設(shè),成都外貿(mào)網(wǎng)站制作,隴川網(wǎng)站建設(shè)費(fèi)用合理。

內(nèi)存解決方案:不要等程序自動(dòng)GC,自己手動(dòng)釋放不需要內(nèi)存。

缺陷:可能會(huì)浪費(fèi)時(shí)間。

如何用vb.net2003讀寫(xiě)內(nèi)存

使用FileStream讀寫(xiě)文件

文件頭:

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

讀文件核心代碼:

byte[] byData = new byte[100];

char[] charData = new char[1000];

try

{

FileStream sFile = new FileStream("文件路徑",FileMode.Open);

sFile.Seek(55, SeekOrigin.Begin);

sFile.Read(byData, 0, 100); //第一個(gè)參數(shù)是被傳進(jìn)來(lái)的字節(jié)數(shù)組,用以接受FileStream對(duì)象中的數(shù)據(jù),第2個(gè)參數(shù)是字節(jié)數(shù)組中開(kāi)始寫(xiě)入數(shù)據(jù)的位置,它通常是0,表示從數(shù)組的開(kāi)端文件中向數(shù)組寫(xiě)數(shù)據(jù),最后一個(gè)參數(shù)規(guī)定從文件讀多少字符.

}

catch (IOException e)

{

Console.WriteLine("An IO exception has been thrown!");

Console.WriteLine(e.ToString());

Console.ReadLine();

return;

}

Decoder d = Encoding.UTF8.GetDecoder();

d.GetChars(byData, 0, byData.Length, charData, 0);

Console.WriteLine(charData);

Console.ReadLine();

寫(xiě)文件核心代碼:

FileStream fs = new FileStream(文件路徑,FileMode.Create);

//獲得字節(jié)數(shù)組

byte [] data =new UTF8Encoding().GetBytes(String);

//開(kāi)始寫(xiě)入

fs.Write(data,0,data.Length);

//清空緩沖區(qū)、關(guān)閉流

fs.Flush();

fs.Close();

2、使用StreamReader和StreamWriter

文件頭:

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

StreamReader讀取文件:

StreamReader objReader = new StreamReader(文件路徑);

string sLine="";

ArrayList LineList = new ArrayList();

while (sLine != null)

{

sLine = objReader.ReadLine();

if (sLine != null!sLine.Equals(""))

LineList.Add(sLine);

}

objReader.Close();

return LineList;

StreamWriter寫(xiě)文件:

FileStream fs = new FileStream(文件路徑, FileMode.Create);

StreamWriter sw = new StreamWriter(fs);

//開(kāi)始寫(xiě)入

sw.Write(String);

//清空緩沖區(qū)

sw.Flush();

//關(guān)閉流

sw.Close();

fs.Close();

===================================================================================

方式一:用FileStream

//實(shí)例化一個(gè)保存文件對(duì)話(huà)框

SaveFileDialog sf = new SaveFileDialog();

//設(shè)置文件保存類(lèi)型

sf.Filter = "txt文件|*.txt|所有文件|*.*";

//如果用戶(hù)沒(méi)有輸入擴(kuò)展名,自動(dòng)追加后綴

sf.AddExtension = true;

//設(shè)置標(biāo)題

sf.Title = "寫(xiě)文件";

//如果用戶(hù)點(diǎn)擊了保存按鈕

if(sf.ShowDialog()==DialogResult.OK)

{

//實(shí)例化一個(gè)文件流---與寫(xiě)入文件相關(guān)聯(lián)

FileStream fs = new FileStream(sf.FileName,FileMode.Create);

//獲得字節(jié)數(shù)組

byte [] data =new UTF8Encoding().GetBytes(this.textBox1.Text);

//開(kāi)始寫(xiě)入

fs.Write(data,0,data.Length);

//清空緩沖區(qū)、關(guān)閉流

fs.Flush();

fs.Close();

}

方式二:用StreamWriter

//實(shí)例化一個(gè)保存文件對(duì)話(huà)框

SaveFileDialog sf = new SaveFileDialog();

//設(shè)置文件保存類(lèi)型

sf.Filter = "txt文件|*.txt|所有文件|*.*";

//如果用戶(hù)沒(méi)有輸入擴(kuò)展名,自動(dòng)追加后綴

sf.AddExtension = true;

//設(shè)置標(biāo)題

sf.Title = "寫(xiě)文件";

//如果用戶(hù)點(diǎn)擊了保存按鈕

if (sf.ShowDialog() == DialogResult.OK)

{

//實(shí)例化一個(gè)文件流---與寫(xiě)入文件相關(guān)聯(lián)

FileStream fs = new FileStream(sf.FileName, FileMode.Create);

//實(shí)例化一個(gè)StreamWriter--與fs相關(guān)聯(lián)

StreamWriter sw = new StreamWriter(fs);

//開(kāi)始寫(xiě)入

sw.Write(this.textBox1.Text);

//清空緩沖區(qū)

sw.Flush();

//關(guān)閉流

sw.Close();

fs.Close();

}

string FileName = Guid.NewGuid().ToString() + ".txt"; //GUID生成唯一文件名

StringBuilder ckpw = new StringBuilder("\"憑證輸出\", \"V800\", \"001\", \"東風(fēng)隨州專(zhuān)用汽車(chē)有限公司\"," + "\"F89自由項(xiàng)16\", \"F90審核日期:\"");

if (!FileIO.IsFolderExists(Server.MapPath("pzsc")))

FileIO.CreaterFolder(Server.MapPath(""), "");

string filePath = Server.MapPath("pzsc") + "\\" + FileName;

System.IO.StreamWriter sw = new System.IO.StreamWriter(filePath, false, Encoding.GetEncoding("GB2312"));//創(chuàng)建的時(shí)候需要指定編碼格式,默認(rèn)是UTF-8,中文顯示亂碼

sw.WriteLine(ckpw.ToString());

sw.Close();

方式三:用BinaryWriter

//實(shí)例化一個(gè)保存文件對(duì)話(huà)框

SaveFileDialog sf = new SaveFileDialog();

//設(shè)置文件保存類(lèi)型

sf.Filter = "txt文件|*.txt|所有文件|*.*";

//如果用戶(hù)沒(méi)有輸入擴(kuò)展名,自動(dòng)追加后綴

sf.AddExtension = true;

//設(shè)置標(biāo)題

sf.Title = "寫(xiě)文件";

//如果用戶(hù)點(diǎn)擊了保存按鈕

if (sf.ShowDialog() == DialogResult.OK)

{

//實(shí)例化一個(gè)文件流---與寫(xiě)入文件相關(guān)聯(lián)

FileStream fs = new FileStream(sf.FileName, FileMode.Create);

//實(shí)例化BinaryWriter

BinaryWriter bw = new BinaryWriter(fs);

bw.Write(this.textBox1.Text);

//清空緩沖區(qū)

bw.Flush();

//關(guān)閉流

bw.Close();

fs.Close();

}

C#緩存流示例------用緩存流復(fù)制文件

C#文件處理操作必須先導(dǎo)入命名空間:using System.IO;

背景:使用VS2005、一個(gè)按鈕、一個(gè)窗體、C#緩存流、把D:\KuGoo\愛(ài)得太多.wma復(fù)制到D:\并更名為love.wma,即:D:\love.wma

在按鈕的Click事件中添加如下代碼:

private void button1_Click(object sender, EventArgs e)

{

//創(chuàng)建兩個(gè)文件流 一個(gè)是源文件相關(guān),另一個(gè)是要寫(xiě)入的文件

FileStream fs = new FileStream(@"D:\KuGoo\愛(ài)得太多.wma",FileMode.Open);

FileStream fs2 = new FileStream(@"D:\love.wma",FileMode.Create);

//創(chuàng)建一個(gè)字節(jié)數(shù)組,作為兩者之間的媒介

//好比兩個(gè)人拿蘋(píng)果,這個(gè)字節(jié)數(shù)組就好比一個(gè)籃子,一個(gè)人作死的把蘋(píng)果送到籃子里面,

//而我就可以作死得拿蘋(píng)果,通過(guò)這個(gè)媒介我們互不干擾,

//不需要互相等待【她往籃子里面放了蘋(píng)果我才可以去拿】,提高了效率

byte[] data = new byte[1024];

//創(chuàng)建兩個(gè)緩沖流,與兩個(gè)文件流相關(guān)聯(lián)

BufferedStream bs = new BufferedStream(fs);

BufferedStream bs2= new BufferedStream(fs2);

//fs作死的讀,fs2作死的寫(xiě),直到fs沒(méi)有字節(jié)可讀fs2就不寫(xiě)了

//好比,一個(gè)人作死的往籃子里面丟蘋(píng)果,另一個(gè)人作死得往籃子里面拿蘋(píng)果,直到籃子里面沒(méi)有蘋(píng)果拿了為止

//即--那個(gè)人沒(méi)有蘋(píng)果往籃子里面放了

while(fs.Read(data,0,data.Length)0)

{

fs2.Write(data,0,data.Length);

fs2.Flush();

}

//關(guān)閉流,好比兩個(gè)人累了,都要休息 呵呵o(∩_∩)o...

fs.Close();

fs2.Close();

}

C#內(nèi)存流示例-----用內(nèi)存流來(lái)讀取圖片

C#文件處理操作必須先導(dǎo)入命名空間:using System.IO;

背景:一個(gè)窗體、一個(gè)pictureBox、一個(gè)lable[沒(méi)有選擇圖片,lable的text為"圖片未選擇"],在pictureBox1的Click事件中添加如下代碼:

private void pictureBox1_Click(object sender, EventArgs e)

{

//實(shí)例化一個(gè)打開(kāi)文件對(duì)話(huà)框

OpenFileDialog op = new OpenFileDialog();

//設(shè)置文件的類(lèi)型

op.Filter = "JPG圖片|*.jpg|GIF圖片|*.gif";

//如果用戶(hù)點(diǎn)擊了打開(kāi)按鈕、選擇了正確的圖片路徑則進(jìn)行如下操作:

if(op.ShowDialog()==DialogResult.OK)

{

//清空文本

this.label1.Text = "";

//實(shí)例化一個(gè)文件流

FileStream fs = new FileStream(op.FileName, FileMode.Open);

//把文件讀取到字節(jié)數(shù)組

byte[] data = new byte[fs.Length];

fs.Read(data, 0, data.Length);

fs.Close();

//實(shí)例化一個(gè)內(nèi)存流---把從文件流中讀取的內(nèi)容[字節(jié)數(shù)組]放到內(nèi)存流中去

MemoryStream ms = new MemoryStream(data);

//設(shè)置圖片框 pictureBox1中的圖片

this.pictureBox1.Image = Image.FromStream(ms);

}

}

vb.net讀取流

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 '寫(xiě)入流

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 類(lèi)型:arraySystem..::.Char[]()[]

此方法返回時(shí),包含指定的字符數(shù)組,該數(shù)組的 index 和 (index + count - 1) 之間的值由從當(dāng)前源中讀取的字符替換。 index 類(lèi)型:System..::.Int32

開(kāi)始寫(xiě)入的 buffer 的索引。 count 類(lèi)型:System..::.Int32

最多讀取的字符數(shù)。

網(wǎng)站題目:vb.net內(nèi)存數(shù)據(jù)流的簡(jiǎn)單介紹
分享URL:http://muchs.cn/article12/hgsedc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供用戶(hù)體驗(yàn)定制開(kāi)發(fā)、虛擬主機(jī)網(wǎng)頁(yè)設(shè)計(jì)公司、關(guān)鍵詞優(yōu)化、網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

小程序開(kāi)發(fā)

網(wǎng)站設(shè)計(jì)公司知識(shí)