1>同时使用了两个 FileStream
对象。sourceStream
用于读取源文件的内容,destinationStream
用于将读取的数据写入目标文件。通过循环读取和写入数据块,我们可以实现文件的复制操作。
2>FileInfo类也是System.IO命名空间中的一个类,提供了用于创建、复制、删除、移动和打开文件的实例方法。
与File类不同,FileInfo类的方法是实例方法,需要通过创建FileInfo对象来调用。
一些常用的方法包括:
FileInfo.Exists:检查当前FileInfo对象表示的文件是否存在。
FileInfo.CopyTo(destinationFilePath):将当前FileInfo对象表示的文件复制到目标路径。
FileInfo.Delete:删除当前FileInfo对象表示的文件。
FileInfo.MoveTo(destinationFilePath):将当前FileInfo对象表示的文件移动到目标路径。
3>Stopwatch
是 .NET 中用于测量时间间隔的类。它提供了高精度的计时功能,可用于计算代码块的执行时间、性能分析和调试等方面。
-
FileStream
实现文件的复制操作
using System;
using System.Diagnostics;
using System.IO;
class Program
{
static void Main(string[] args)
{
string sourceFilePath = "txttestA.txt";
string destinationFilePath = "txttestAdestination.txt";
// 获取源文件大小(以MB为单位)
double fileSizeInMB = GetFileSizeInMB(sourceFilePath);
// 复制文件并计算所需时间
Stopwatch stopwatch = Stopwatch.StartNew();
CopyLargeFile(sourceFilePath, destinationFilePath);
stopwatch.Stop();
Console.WriteLine($"文件复制完成。源文件大小:{fileSizeInMB} MB");
Console.WriteLine($"复制所需时间:{stopwatch.Elapsed.TotalMilliseconds} 毫秒");
Console.ReadKey();
}
static double GetFileSizeInMB(string filePath)
{
FileInfo fileInfo = new FileInfo(filePath);
long fileSizeInBytes = fileInfo.Length;
double fileSizeInMB = (double)fileSizeInBytes / (1024 * 1024); // 转换为MB
return fileSizeInMB;
}
static void CopyLargeFile(string sourceFilePath, string destinationFilePath)
{
const int bufferSize = 65536; // 64KB
using (FileStream sourceStream = new FileStream(sourceFilePath, FileMode.Open, FileAccess.Read))
using (FileStream destinationStream = new FileStream(destinationFilePath, FileMode.Create, FileAccess.Write))
{
byte[] buffer = new byte[bufferSize];
int bytesRead;
while ((bytesRead = sourceStream.Read(buffer, 0, buffer.Length)) > 0)
{
destinationStream.Write(buffer, 0, bytesRead);
}
}
}
}
// 文件复制完成。源文件大小:13.1021614074707 MB
// 复制所需时间:35.1329 毫秒
-
补充FileInfo
# 遍历目录中的文件:
string directoryPath = "path/to/directory";
DirectoryInfo directoryInfo = new DirectoryInfo(directoryPath);
FileInfo[] files = directoryInfo.GetFiles();
foreach (FileInfo file in files)
{
string fileName = file.Name;
// 对每个文件执行操作
}
# 获取文件信息:
string filePath = "path/to/file.txt";
FileInfo fileInfo = new FileInfo(filePath);
string fileName = fileInfo.Name; // 获取文件名(包括扩展名)
string fileDirectory = fileInfo.DirectoryName; // 获取文件所在目录的路径
long fileSize = fileInfo.Length; // 获取文件大小(字节数)
DateTime creationTime = fileInfo.CreationTime; // 获取文件创建时间
DateTime lastWriteTime = fileInfo.LastWriteTime; // 获取文件上次写入时间
bool fileExists = fileInfo.Exists; // 检查文件是否存在
-
计算方法的执行时间:
TimeSpan MeasureMethodExecutionTime(Action method)
{
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); // 启动计时器
method.Invoke(); // 调用需要测量时间的方法
stopwatch.Stop(); // 停止计时器
return stopwatch.Elapsed; // 返回经过的时间
}
void MyMethod()
{
// 方法体
}
TimeSpan executionTime = MeasureMethodExecutionTime(MyMethod);
Console.WriteLine("方法执行时间: " + executionTime);
# 进行性能测试:
int iterations = 1000000;
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start(); // 启动计时器
for (int i = 0; i < iterations; i++)
{
// 执行需要测试性能的代码
}
stopwatch.Stop(); // 停止计时器
TimeSpan elapsed = stopwatch.Elapsed; // 获取经过的时间
double averageTimePerIteration = elapsed.TotalMilliseconds / iterations;
Console.WriteLine("平均每次迭代时间: " + averageTimePerIteration + " 毫秒");