FileDiff2 优化版






3.80/5 (3投票s)
一个文件差异比较工具。
引言
这个应用程序非常基础,它使用 FileStream
对象来执行其任务。
使用代码
ASCIIEncoding Encode = new ASCIIEncoding();
// Open the files
//
FileStream streamA = File.OpenRead(args[0]);
FileStream streamB = File.OpenRead(args[1]);
// Get the stream length
// (so we don't have to caluculate this a million times)
//
long lenA = streamA.Length - 1;
long lenB = streamB.Length - 1;
// Read the bytes
//
int byteA;
int byteB;
do
{
// Read the streams
//
byteA = streamA.ReadByte();
byteB = streamB.ReadByte();
// Are they the same
//
if (byteA != byteB)
{
// Remember where we parked the car
//
long startPos = streamB.Position;
// Read streamB until we = StreamA
//
do
{
byteB = streamB.ReadByte();
}
while (byteA != byteB && streamB.Position <= lenB);
// How long is the difference?
//
long length = streamB.Position - startPos;
// Read the bytes
//
byte[] theseBytes = new byte[length];
streamB.Seek(length * -1, SeekOrigin.Current);|
streamB.Read(theseBytes, 0, (int)length);
Console.WriteLine("Pos:{0}, Len:{1}, Str:{2}", startPos,
length, Encode.GetString(theseBytes));
}
}
while (streamA.Position <= lenA && streamB.Position <= lenB);
streamA.Close();
streamB.Close();
历史
- 2009年8月12日:编写完成。
- 2009年8月13日:重写以使其更明确,并且我修改了文件查找和一些变量,将运行时间从 57 毫秒降低到 27 毫秒。