c# Stream to File的知识点
个人倾向使用File.WriteAllByte写入文件:
//Stream to File
MemoryStream ms=...Stream;
ms.Position = ;
byte[] buffer = new byte[ms.Length];
ms.Read(buffer, , (int)ms.Length);
File.WriteAllBytes(filepath, buffer);//写入文件
StreamRead,StreamWrite,SreamRead,以及File的部分操作
// StreamRead,StreamWrite,File只能操作文本文件
//FileStream可以操作所有格式 包括文本,文件,图片,视频
//SreamRead读取文件
using (StreamReader reader = new StreamReader(filepath))
{
//reader.ReadLine();读取第一行
string data = reader.ReadToEnd();//读取所有
}
//StreamWrite写入文件
using (StreamWriter writer = new StreamWriter(filepath))
{
//writer.Write("写入的内容");
//也可以如下面一行一行写入
writer.WriteLine("");
writer.WriteLine("");
writer.WriteLine(picName);
writer.WriteLine(txtCount.Text);
} //File即可以读取文本 也可以写入文本
//读取
string data = File.ReadAllText(filepath);
string[] datas = File.ReadAllLines(filepath);
//写入
string[] ss = new string[] {"arr1","arr2" };
File.WriteAllLines(filepath, ss);
File.WriteAllText(filepath, "\r\n换行\r\n");
//附加
File.AppendAllLines(filepath,ss);
File.AppendAllText(filepath,str); //FileStream可以操作所有格式 包括文本,文件,图片,视频
//FileStream读取
string ss = null;
byte[] buffer = new byte[ * * ]; //针对大文件1M1M的读取
using (FileStream sr = new FileStream(sfilepath, FileMode.OpenOrCreate, FileAccess.Read))
{
int sss = sr.Read(buffer, , buffer.Length);
ss = Encoding.Default.GetString(buffer, , sss);
}
//FileStream写入
using (FileStream fsWrite = new FileStream(filepath, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer = Encoding.Default.GetBytes(str);
fsWrite.Write(buffer, , buffer.Length);
}
Stream 和 File 之间的转换
/// <summary>
/// 将 Stream 写入文件
/// </summary>
public void StreamToFile(Stream stream,string filepath)
{
// 把 Stream 转换成 byte[]
byte[] bytes = new byte[stream.Length];
stream.Read(bytes, , bytes.Length);
// 设置当前流的位置为流的开始
stream.Seek(, SeekOrigin.Begin);
// 把 byte[] 写入文件
FileStream fs = new FileStream(filepath, FileMode.Create);
BinaryWriter bw = new BinaryWriter(fs);
bw.Write(bytes);
bw.Close();
fs.Close();
}
/// <summary>
/// 从文件读取 Stream
/// </summary>
public Stream FileToStream(string filepath)
{
// 打开文件
FileStream fileStream = new FileStream(filepath, FileMode.Open, FileAccess.Read, FileShare.Read);
// 读取文件的 byte[]
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, , bytes.Length);
fileStream.Close();
// 把 byte[] 转换成 Stream
Stream stream = new MemoryStream(bytes);
return stream;
}
文件复制(当然File.Copy更常用,这个是偶然中使用到整理的)
public static void CopyFile(string soucre, string target)
{
//读取的流
using (FileStream fsRead = new FileStream(soucre, FileMode.Open, FileAccess.Read))
{
//写入的流
using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer = new byte[ * * ];
while (true)
{
int r = fsRead.Read(buffer, , buffer.Length);
//0代表已经读取完
if (r == )
{
break;
}
fsWrite.Write(buffer, , r);
}
}
}
}
c# Stream to File的知识点的更多相关文章
- Java-Runoob:Java Stream、File、IO
ylbtech-Java-Runoob:Java Stream.File.IO 1.返回顶部 1. Java 流(Stream).文件(File)和IO Java.io 包几乎包含了所有操作输入.输出 ...
- laravel he stream or file "..laravel-2019-02-14.log" could not be opened: failed to open stream: Permission denied
错误:The stream or file "/var/www/jianshu/storage/logs/laravel-2019-02-14.log" could not be ...
- Java入门 - 语言基础 - 20.Stream和File和IO
原文地址:http://www.work100.net/training/java-stream-file-io.html 更多教程:光束云 - 免费课程 Stream和File和IO 序号 文内章节 ...
- 流Stream 文件File 流IO
Java 流(Stream).文件(File)和IO Java.io 包几乎包含了所有操作输入.输出需要的类.所有这些流类代表了输入源和输出目标. Java.io 包中的流支持很多种格式,比如:基本类 ...
- 14、Java文件操作stream、File、IO
1.文件操作涉及到的基本概念 File File类 是文件操作的主要对象中文意义就是 文件 顾名思意 万物皆文件,在计算上看到的所有东西都是文件保存,不管是你的图片.视频.数据库数据等等都是按照基本的 ...
- [Node.js] 03 - Buffer, Stream and File IO
fs 模块,视频教学 os 模块,视频教学,api doc Buffer类 创建 Buffer 类 // 创建一个长度为 10.且用 0 填充的 Buffer. const buf1 = Buffer ...
- C#图像处理:Stream 与 byte[] 相互转换,byte[]与string,Stream 与 File 相互转换等
C# Stream 和 byte[] 之间的转换 一. 二进制转换成图片 MemoryStream ms = new MemoryStream(bytes); ms.Position = 0; Ima ...
- 转:Java.file
类 java.io.File 的使用 使用 File 的软件包 java.awt 包含用于创建用户界面和绘制图形图像的所有类. java.io 通过数据流.序列化和文件系统提供系统输入和输出. jav ...
- [转]c# xml.Load() locking file on disk causing errors
本文转自:http://stackoverflow.com/questions/1812598/c-sharp-xml-load-locking-file-on-disk-causing-errors ...
随机推荐
- 《剑指Offer》题六十一~题六十八
六十一.扑克牌中的顺子 题目:从扑克牌中随机抽5张牌,判断是不是一个顺子,即这5张牌是不是连续的.2~10为数字本身,A为1,J为11,Q为12,K为13,而大.小王可以看成任意数字. 六十二.圆圈中 ...
- java—连连看-实现消除
实现消除 1.Chess.java package Linkup; /** * 棋子封装类 * * @author laixl * */ public class Chess { // 图片的 状态 ...
- 【IdentityServer4文档】- 欢迎来到 IdentityServer4
欢迎来到 IdentityServer4 IdentityServer4 是一款包含和实现了 OpenID Connect 和 OAuth 2.0 协议的,适用于 ASP.NET Core 的框架 . ...
- TCP系列40—拥塞控制—3、慢启动和拥塞避免概述
本篇中先介绍一下慢启动和拥塞避免的大概过程,下一篇中将会给出多个linux下reno拥塞控制算法的wireshark示例,并详细解释慢启动和拥塞避免的过程. 一.慢启动(slow start) 一个T ...
- 在 Range 对象中,Min (14)必须小于或等于 max (-1)。
DataTable dt = ds.Tables[]; DataRow[] drs = dt.Select("Id=" + categoryID ); 解决方法:将参数用单引号阔起 ...
- SQL Server之看懂执行计划
在SQL Server中,选中一段SQL按Ctrl+L,就可以查看它的执行计划. 上面是一个执行计划的实例,在SQL Server中,执行计划是从右往左看的. SQL Server中,查找数据的方式有 ...
- 某一线互联网公司前端面试题总结css部分
1,css3选择器 :not(selector) 选择页面内所有type!=text的类型: input:not([type=text]){ color: red; font-weight: bold ...
- try catch finally 与continue的使用
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- ASP.NET MVC 多语言解决方案
1:打开VS,新建ASP.NET MVC4项目 2:创建一个放本地化资源的文件夹并命名为"Language",右键选择添加新项,选择资源文件并命名为"Com" ...
- overflow:scroll 在ios 滚动卡顿
使用 -webkit-overflow-scrolling 属性控制元素在移动设备上是否使用滚动回弹效果. 值 auto 使用普通滚动, 当手指从触摸屏上移开,滚动会立即停止. touch 使用具有回 ...