C# 文件流FileStream 实现多媒体文件复制 StreamReader StreamWriter 读取写入文本
2024-10-01 16:04:38
#region 实现多媒体文件的复制
string source = @"F:\123\source.avi";//源文件路径
string target = @"F:\123\test.avi";//复制后的文件路径
CopyFile(source, target);
Console.WriteLine("复制成功");
#endregion
Console.ReadKey();
public static void CopyFile(string source, string target)
{
//创建一个负责读取的流
using (FileStream fsRead = new FileStream(source, FileMode.Open, FileAccess.Read))
{
//创建一个负责写入的流
using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer = new byte[1024 * 1024 * 5];
//因为文件可能比较大所以在读取的时候应该用循坏去读取
while (true)
{
//返回本次实际读取到的字节数
int r = fsRead.Read(buffer, 0, buffer.Length);
if (r == 0)
{
break;
}
fsWrite.Write(buffer, 0, r);//写入
}
}
}
//使用StreamReader读取文本文件
using (StreamReader sr = new StreamReader(@"F:\123.txt", Encoding.Default))
{
while (!sr.EndOfStream)
{
Console.WriteLine(sr.ReadLine());
}
}
//使用StreamWrite写入文本文件
using (StreamWriter sw = new StreamWriter(@"F:\123.txt"))
{
sw.Write("写入内容");
}
最新文章
- 【初探Spring】------Spring IOC(三):初始化过程---Resource定位
- .net mvc中json的时间格式
- iOS-推送通知详解
- selenium-webdriver(python) (十四) -- webdriver原理
- Python基本数据类型之list
- Oracle创建表格报ORA-00906:缺失左括号错误解决办法
- 【leetcode❤python】299. Bulls and Cows
- iOS开发-表视图的使用
- (转) 如何在JavaScript与ActiveX之间传递数据1
- ECshop网店系统百万级商品量性能优化-加快首页访问速度
- Fetch的使用
- openerp 产品图片的批量写入
- WCF入门教程系列六
- Scala学习文档-样本类与模式匹配(match,case,Option)
- Object 类
- windowns10安装httpd
- 解决iPhone Safari 兼容性CSS背景显示不全问题
- python修炼第四天
- Leetcode 11.盛最多水的容器 By Python
- hibernate中的addEntity setResultTransformer的比较