導航:首頁 > 文件教程 > filestream讀取大文件

filestream讀取大文件

發布時間:2022-07-06 19:25:50

A. 如何讀取大文件

visual c++ 如何:讀取二進制文件 下面的代碼示例演示如何從文件中讀取二進制數據。使用了 system.io 命名空間中的兩個類:filestream 和 binaryreader。filestream 表示實際的文件。binaryreader 為允許二進制訪問的流提供介面。 下面的代碼示例使用由如何:編寫二進制文件中的代碼創建的稱為 data.bin 的文件。 示例 // binary_read.cpp // compile with: /clr #using<system.dll> using namespace system; using namespace system::io; int main() { string^ filename = "data.bin"; try { filestream^ fs = gcnew filestream(filename, filemode::open); binaryreader^ br = gcnew binaryreader(fs); console::writeline("contents of {0}:", filename); while (br->basestream->position < br->basestream->length) console::writeline(br->readint32().tostring()); fs->close( ); } catch (exception^ e) { if (dynamic_cast<filenotfoundexception^>(e)) console::writeline("file '{0}' not found", filename); else console::writeline("exception: ({0})", e); return -1; } return 0; } ★★補充★★ 手上的一個vb項目(過程中發現,.net果然是好啊),需要在一個activex中實現http下載功能, 是採用internetreadfile這個api來實現,一開始的代碼 是這么寫的 function gethttpdownload(surl as string) as boolen dim s as string dim hopen as long dim hopenurl as long dim bdoloop as boolean dim bret as boolean dim sreadbuffer as string * 2048 dim lnumberofbytesread as long hopen = internetopen(scuseragent, internet_open_type_preconfig, vbnullstring, vbnullstring, 0) hopenurl = internetopenurl(hopen, surl, vbnullstring, 0, internet_flag_reload, 0) bdoloop = true do while bdoloop sreadbuffer = vbnullstring bret = internetreadfile(hopenurl, sreadbuffer, len(sreadbuffer), lnumberofbytesread) s = s & left$(sreadbuffer, lnumberofbytesread) if not cbool(lnumberofbytesread) then bdoloop = false loop filename = "e:\bitspirit\torrent\121212.torrent" f1 = freefile open filename for binary as f1 put f1, , s close f1 if hopen <> 0 then internetclosehandle (hopen) gethttpdownload = true end function 上面方法,用來獲取伺服器上的文本類型的文件一點問題,都沒有,但是用來下載二進制文件的時候 就出現問題了,裡面的數據怎麼也不對,研究了下載下來的文件後發現,問題外話應該是在接收數據的變數是個string的定長字元串上。但是在網上查了好久,甚至m$ msdn上的一個用vb來實現下載的程也是用 string類型來接收數據的而且網上的代碼寫法,基本上也都是這個樣,好來才好現,都是從msdn上的哪個常式上演變過來的. 想如果能用一個byte數組來代替定長字元串,哪可能就沒有問題了,但是查看了一個vb 對internetreadfile的申明 public declare function internetreadfile lib "wininet.dll" (byval hfile as long, byval sbuffer as string, byval lnumbytestoread as long, lnumberofbytesread as long) as integer 發現其定義byval sbuffer as string 看來只能用string了,在網上查找過程中,發現人家用vc寫的程序中這人參數可以是其它的,所以查看了一下internetreadfile的原型。發現的確可以, 所以 把internetreadfile的定義修改了一下,為了通用, 為新的internetreadfile定義了一個別名。internetreadfilebyte申明如下: public declare function internetreadfilebyte lib "wininet.dll" alias "internetreadfile" (byval hfile as long, byref sbuffer as byte, byval lnumbytestoread as long, lnumberofbytesread as long) as integer 試了一下的確可以,重新修改函數,(在這過程中發現,如果要取到正確數據,還必須取得文件大小。所以增加了httpqueryinfo的定義)最後完整的函數 function filedownload(surl as variant) as boolean dim b(99) as byte dim endbyte() as byte dim s as string dim hopen as long dim hopenurl as long dim bdoloop as boolean dim bret as boolean dim bbuffer as byte dim sreadbuffer as string dim filename as string dim lnumberofbytesread as long dim f1 as integer dim strsize as string dim size as long strsize = string$(1024, " ") f1 = freefile sttotal = vbnullstring filename = "e:\bitspirit\torrent\121212.torrent" open filename for binary as f1 hopen = internetopen(scuseragent, internet_open_type_preconfig, vbnullstring, vbnullstring, 0) hopenurl = internetopenurl(hopen, surl, vbnullstring, 0, internet_flag_reload, 0) bdoloop = true httpqueryinfo hopenurl, http_query_content_length or http_query_flag_number, byval strsize, len(strsize), 0 size = clng(trim(strsize)) for j = 1 to size \ 100 bdoloop = internetreadfilebyte(hopenurl, b(0), 100, lnumberofbytesread) put f1, , b if not cbool(lnumberofbytesread) then exit for next if size mod 100 <> 0 then tmp = (size mod 100) - 1 redim endbyte(tmp) bdoloop = internetreadfilebyte(hopenurl, endbyte(0), tmp + 1, lnumberofbytesread) put f1, , endbyte end if if hopenurl <> 0 then internetclosehandle (hopenurl) if hopen <> 0 then internetclosehandle (hopen) close #1 filedownload = true end function 測試了一下,完全成功:)

B. 初學delphi,看到tony的FastFileStream,用於大文件快速讀取,但不知如何調用,哪位大俠提供一個示例

這個東西用法跟普通的filesystem差不多,你搜索一下filesystem用法就知道了,比如說從一個文件中第四個位元組開始讀取8個位元組至數組,其它的象seek、write之類的方法也類似於filesystem

var
buffer:array[0..7]ofchar;
f1:TFastFileStream;
begin
f1:=TFastFileStream.Create('e:mongodb-win32-x86_64-2.2.2.zip');
f1.Position:=4;
f1.Read(buffer,sizeof(buffer));
f1.Free;
showmessage('yes');
end;

C. C#FileStream循環讀取大文件有什麼好處

雖然是在 while 循環內實現讀寫,但不能說是循環讀取(循環讀取說的是讀到尾後有從頭讀),而是分段讀取
分段讀取可以減少緩存空間的內存開銷,合適的緩存空間可提高硬碟的尋道效率

D. FLEX裡面利用FileStream類來讀取文件夾裡面文件的問題

有其他語言獲得數據在穿給flex
這個不是幾句話說的清楚的
我給你一個php的方法
用amfphp
你可以看這個

http://wenku..com/view/df20191c59eef8c75fbfb346.html

E. 如何將filestream讀取某文件的數據放到一個byte[]中

byte[] basicbyte = File.ReadAllBytes("1.txt");

F. C# 讀取文件的問題:file類的ReadAllText、WriteAllText與流Filestream的關系

我們看不到。net Framework 的源代碼抄,但猜測ReadAllText()基本上應該也是用Filestream的read方式實現的。多一個方法就可以讓你少寫很多行的代碼,不用處理Filestream的Dispose。這就是寫library的人比較聰明之處吧。

當然具體比較兩個方法,ReadAllText()是一個簡單的方法,你一次讀出所有的內容。而直接read Filestream,你有更多的控制,但也需要自己初始化Filestream,記得Dispose FileStream.

簡單說吧,ReadAllText()像月票,Read 像每次坐車現買車票。

WriteAllText, 和 Write 方法和前面的一對類似。

G. C#中的FileStream類文件讀取

把文件流里的內容放到集合里保存
然後再從集合中讀取數據內容
用[]找到你所要讀的行

H. c# Filestream 如何讀取內嵌資源文件

內嵌後vs會為對應資源直接生成一個成員屬性,如果文件是圖片就返回圖片類型,如果是其他不識別的文件就返回byte[] 類型
你直接調用即可,無需重新寫讀取方法

I. C#讀取大容量Txt文件的問題

你這代碼有幾個問題:

首先,你這記錄論「條」,實際的條數只是總位元組數除ChunkSize的值,個人建議你這要是計數的是行數就更好了,這可以用StreamReader.ReadLine實現,如果你的文件10M,文件大小就是10*1024*1024,你的ChunkSize是102400,記錄數自然是10*1024*1024/102400=102.4,你這142條記錄應該有10M多一些,沒什麼不對的

其次,多線程輸出到RichTextBox顯然畫蛇添足,反而會導致RichTextBox中的內容順序跟文件可能不一致,因為開啟多線程之後順序是不一定的

第三,多線程開銷是很大的,你這短時間內while循環那麼多次就開啟了大量的多線程,每個線程就給RichTextBox賦值這太浪費了

至於性能問題,跟其他幾位說的一樣,瓶頸在磁碟讀寫,你這多線程處理RichTextBox沒什麼意義

constlongChunkSize=102400;
byte[]bytcontent=newbyte[ChunkSize];
FileStreamfs=newFileStream(@"C:unintall.log",FileMode.Open);
inti=0;
while(fs.Read(bytcontent,0,bytcontent.Length)>0)
{
i++;
Console.Write(System.Text.Encoding.Default.GetString(bytcontent));
}
Console.WriteLine("共有:"+i+"條記錄!");

下面是ReadLine的

using(StreamReadersr=newStreamReader(@"C:unintall.log",System.Text.Encoding.Default))
{
while(sr.Peek()>=0)
{
Console.WriteLine(sr.ReadLine());
}
}

參考

https://msdn.microsoft.com/zh-cn/library/system.io.streamreader.readline.aspx

閱讀全文

與filestream讀取大文件相關的資料

熱點內容
百度離線的js文件在哪 瀏覽:992
穿越到抗戰擁有系統的小說 瀏覽:113
開數據怎麼開的 瀏覽:35
島國能看的網站 瀏覽:960
win10servicing文件夾 瀏覽:989
午夜影院0660 瀏覽:323
法國love手機在線 瀏覽:433
抖音免費在線觀看 瀏覽:799
韓國倫理電影愛人免費在線播放 瀏覽:195
為什麼電腦微信文件發送不出去 瀏覽:573
新入職女社員女演員叫什麼 瀏覽:700
可搜索 網站 在線觀看 瀏覽:816
對數據真實性負有什麼權 瀏覽:604
elonafix版本錯誤 瀏覽:70
百度網路推廣的形式 瀏覽:691
word左邊顯示提綱 瀏覽:810
台灣最好看的三極武工片 瀏覽:98
2021最新電影免費觀看的網址 瀏覽:629
韓國推理片在線播放 瀏覽:110
推薦韓日推理電影 瀏覽:989

友情鏈接