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 ...
随机推荐
- Advanced Fruits (最大公共子序列的路径打印)
The company "21st Century Fruits" has specialized in creating new sorts of fruits by trans ...
- 3dContactPointAnnotationTool开发日志(十九)
增加了输出接触点信息到文件功能.
- STL--heap概述:make_heap,sort_heap,pop_heap,push_heap
heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制. 而这个实现机制中的max-hea ...
- netbeans调试配置
apache端口8050,xdebug端口9000 1.把项目放到apache的htdocs下(一定要放在htdocs上,要么调试的时候xdebug会一直卡在“等待连接中”) 2.把php_xdebu ...
- js 控制
js 制动控制 代码 是 :setInterval(function(){$(".egg").click();},1000); 使用方法:调出浏览器放控制台(console),一般 ...
- js 复制到剪贴板 兼容还得自己想办法
clipboard.js https://github.com/zenorocha/clipboard.js/ 主要问题还是ie8, 可以使用ie 特有的方法 if (window.clipboard ...
- C++中Map的使用 (个人简单的对于String的使用)
#include<map> #include<iostream> #include<string> using namespace std; int main() ...
- 1193: [HNOI2006]马步距离
1193: [HNOI2006]马步距离 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2027 Solved: 915[Submit][Statu ...
- 【刷题】BZOJ 2179 FFT快速傅立叶
Description 给出两个n位10进制整数x和y,你需要计算x*y. Input 第一行一个正整数n. 第二行描述一个位数为n的正整数x. 第三行描述一个位数为n的正整数y. Output 输出 ...
- 【刷题】BZOJ 1003 [ZJOI2006]物流运输
Description 物流公司要把一批货物从码头A运到码头B.由于货物量比较大,需要n天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运输过程实施严格 ...