suo4.cs
资源名称:Visual.rar [点击查看]
上传用户:yiyuerguo
上传日期:2014-09-27
资源大小:3781k
文件大小:2k
源码类别:
C#编程
开发平台:
Others
- // suo3.cs
- // 示例文件:cscs.txt
- using System;
- using System.IO;
- // 建立类文件
- // 就好像是一个数组一样
- public class FileByteArray
- {
- //建立一个流
- Stream stream;
- //用FileStream类实现对字符流的输入
- public FileByteArray(string fileName)
- {
- stream = new FileStream(fileName, FileMode.Open);
- }
- //当使用流文件结束后要关闭流
- public void Close()
- {
- stream.Close();
- stream = null;
- }
- // Indexer to provide read/write access to the file.
- public byte this[long index] // long is a 64-bit integer
- {
- // 读取一个字节 然后返回它
- get
- {
- byte[] buffer = new byte[1];
- stream.Seek(index, SeekOrigin.Begin);
- stream.Read(buffer, 0, 1);
- return buffer[0];
- }
- // 写入一个字节
- set
- {
- byte[] buffer = new byte[1] {value};
- stream.Seek(index, SeekOrigin.Begin);
- stream.Write(buffer, 0, 1);
- }
- }
- // 得到流的总长度
- public long Length
- {
- get
- {
- return stream.Seek(0, SeekOrigin.End);
- }
- }
- }
- // Demonstrate the FileByteArray class.
- // Reverses the bytes in a file.
- public class Reverse
- {
- public static void Main(String[] args)
- {
- // 检测这个文本文件是否存在
- if (args.Length == 0)
- {
- Console.WriteLine("indexer <filename>");
- return;
- }
- //将文本文件转到字节数组内
- FileByteArray file = new FileByteArray(args[0]);
- long len = file.Length;
- // 交换有为的字节达到翻转的效果
- for (long i = 0; i < len / 2; ++i)
- {
- byte t;
- //file索引器交换数的位置
- t = file[i];
- file[i] = file[len - i - 1];
- file[len - i - 1] = t;
- }
- file.Close();
- }
- }