//循环的目的为了按照字节反转文件。using System;
using System.IO;public class TestIndex
{
public static void Main(String[] args)
{
if(0 == args.Length)
{
Console.WriteLine("please input filename");
return ;
}
IndexSample.FileByteArray cReverse = new IndexSample.FileByteArray(args[0]);
long lLen = cReverse.FileLength;
//reverse file
for(long lLoop = 0; lLoop < lLen/2; lLoop++)
{
byte btTemp = cReverse[lLoop];
cReverse[lLoop] = cReverse[lLen-lLoop-1];
cReverse[lLen-lLoop-1] = btTemp;
}
cReverse.Close();
}
}namespace IndexSample
{
/// <summary>
/// FileByteArray is used to access a file as a byte array.
/// </summary>
public class FileByteArray
{
private Stream cStream; public FileByteArray(String strFileName)
{
cStream = new FileStream(strFileName, FileMode.Open);
}
//close Stream at the end.
public void Close()
{
cStream.Close();
cStream = null;
}
//provide file's IO access operate
public byte this[long lIndex]
{
get
{
byte[] btBuffer = new byte[1];
cStream.Seek(lIndex, SeekOrigin.Begin);
cStream.Read(btBuffer, 0, 1);
return btBuffer[0];
}
set
{
byte[] btBuffer = new byte[1] {value};
cStream.Seek(lIndex, SeekOrigin.Begin);
cStream.Write(btBuffer, 0, 1);
}
}
public long FileLength
{
get
{ return cStream.Seek(0, SeekOrigin.End);}
}
}
}
using System.IO;public class TestIndex
{
public static void Main(String[] args)
{
if(0 == args.Length)
{
Console.WriteLine("please input filename");
return ;
}
IndexSample.FileByteArray cReverse = new IndexSample.FileByteArray(args[0]);
long lLen = cReverse.FileLength;
//reverse file
for(long lLoop = 0; lLoop < lLen/2; lLoop++)
{
byte btTemp = cReverse[lLoop];
cReverse[lLoop] = cReverse[lLen-lLoop-1];
cReverse[lLen-lLoop-1] = btTemp;
}
cReverse.Close();
}
}namespace IndexSample
{
/// <summary>
/// FileByteArray is used to access a file as a byte array.
/// </summary>
public class FileByteArray
{
private Stream cStream; public FileByteArray(String strFileName)
{
cStream = new FileStream(strFileName, FileMode.Open);
}
//close Stream at the end.
public void Close()
{
cStream.Close();
cStream = null;
}
//provide file's IO access operate
public byte this[long lIndex]
{
get
{
byte[] btBuffer = new byte[1];
cStream.Seek(lIndex, SeekOrigin.Begin);
cStream.Read(btBuffer, 0, 1);
return btBuffer[0];
}
set
{
byte[] btBuffer = new byte[1] {value};
cStream.Seek(lIndex, SeekOrigin.Begin);
cStream.Write(btBuffer, 0, 1);
}
}
public long FileLength
{
get
{ return cStream.Seek(0, SeekOrigin.End);}
}
}
}
解决方案 »
- 请DataGrid中的一列的数据如何变成xxxx,xxxxx,xxxxx这种形式的字符串?
- 像csdn、奥运购门票网站、taobao、新浪、网易是怎样架服务器的哟?
- C# 取系统时间不显示秒
- txtbox输入内容限制??.NET2003 C# help!!在线等待。
- sql语句问题,update中字段内容等于字符串相加
- 超简单的access查询参数格式问题
- c#中oracle,插入数据,为什么老出错,麻烦大家了?????????
- 急!急!急!急!donet中为什么不能新建项目了??????????
- 如何在C#中调有参数的MSSQL存储过程??急.在线等
- SendKey.Send 有时不起作用
- 请问如何把string转换为Datetime格式
- c#中怎么生成excel
{
byte btTemp = cReverse[lLoop];
cReverse[lLoop] = cReverse[lLen-lLoop-1];
cReverse[lLen-lLoop-1] = btTemp;
}
你是說這裡的/2嗎?很明顯,循環是要把cReverse數組中的數據首尾調換,這樣的話,隻要循環一半就可以把數據調換完全。