#region 实现多媒体文件的复制
string source = @"F:123source.avi";//源文件路径
string target = @"F:123 est.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("写入内容");
}