網(wǎng)站首頁 編程語言 正文
在拿到一個 Stream 如何優(yōu)雅將這個 Stream 保存到代碼
最優(yōu)雅的方法應(yīng)該是通過 CopyTo 或 CopyToAsync 的方法
using (var fileStream = File.Create("C:\\lindexi\\File.txt")) { inputStream.Seek(0, SeekOrigin.Begin); iputStream.CopyTo(fileStream); }
這里的?inputStream.Seek(0, SeekOrigin.Begin);
?不一定需要,請根據(jù)你自己的需求,如你只需要將這個 Stream 的從第10個byte開始復(fù)制等就不能采用這句代碼
用異步方法會讓本次寫入的時間長一點,但是會讓總體性能更好,讓 CPU 能處理其他任務(wù)
using (var fileStream = File.Create("C:\\lindexi\\File.txt")) { await iputStream.CopyToAsync(fileStream); }
注意使用 CopyToAsync 記得加上 await 哦,執(zhí)行到這句代碼的時候,就將執(zhí)行交給了 IO 了,大部分的 IO 處理都不需要 CPU 進行計算,這樣能達(dá)到總體性能更好
另外如果 iputStream 是外面?zhèn)魅氲模敲次也唤ㄗh在這個方法里面釋放,為什么呢?我用的好好的一個Stream傳入一個業(yè)務(wù)就被干掉了
其次的方法是自己控制內(nèi)存復(fù)制緩存,此方法將會多出一次內(nèi)存復(fù)制
public static void CopyStream(Stream input, Stream output) { byte[] buffer = new byte[1024]; int len; while ( (len = input.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, len); } } // 使用方法如下 using (Stream file = File.Create("C:\\lindexi\\File.txt")) { CopyStream(input, file); }
此方法的作用就是讓你修改?new byte[1024]
?的值,讓你可以控制復(fù)制的緩存
接下來就是一些不推薦的方法了,但是寫的時候方便
using (var stream = new MemoryStream()) { input.CopyTo(stream); File.WriteAllBytes(file, stream.ToArray()); }
上面這個方法將會復(fù)制兩次內(nèi)存,而且如果 input 這個資源長度有 1G 就要占用 2G 的資源
和上面差不多的是申請一個大的緩存,如下面代碼:
public void SaveStreamToFile(string fileFullPath, Stream stream) { if (stream.Length == 0) return; using (FileStream fileStream = System.IO.File.Create(fileFullPath, (int)stream.Length)) { byte[] bytesInStream = new byte[stream.Length]; stream.Read(bytesInStream, 0, (int)bytesInStream.Length); fileStream.Write(bytesInStream, 0, bytesInStream.Length); } }
從效率和代碼的優(yōu)雅其實都不如 CopyTo 方法,而且因為 stream.Length 作為長度沒有決定緩存,所以也不如第二個方法
public void SaveStreamToFile(Stream stream, string filename) { using(Stream destination = File.Create(filename)) { Write(stream, destination); } } public void Write(Stream from, Stream to) { for(int a = from.ReadByte(); a != -1; a = from.ReadByte()) { to.WriteByte( (byte) a ); } }
原文鏈接:https://blog.csdn.net/Pei_hua100/article/details/126159850
相關(guān)推薦
- 2022-06-04 R語言批量讀取某路徑下文件內(nèi)容的方法_R語言
- 2022-08-20 C#?Chart折線圖使用鼠標(biāo)滾輪放大、縮小和平移曲線方式_C#教程
- 2023-03-23 一文帶你了解Python中的type,isinstance和issubclass_python
- 2022-04-22 arm-linux使用qt開發(fā)并加入openssl
- 2022-07-19 Linux cat more grep head tail cut uniq sort tr命令詳解
- 2022-10-29 SQL?Server主鍵約束(PRIMARY?KEY)_MsSql
- 2021-12-15 CentOS下更新SQLite版本_SQLite
- 2022-12-22 Python?Flask框架實現(xiàn)Proteus仿真Arduino與網(wǎng)頁數(shù)據(jù)交互_python
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支