大家新年快乐吖 (¯▽¯;)
这是一个包含所有代码的示例 可直接运行,主要包含了文件读写时二进制数据流和字符数据的处理
并且我都写了很详细的注释 方便大家也方便自己学习
:
using System; using System.Net.NetworkInformation; using System.Text; using System.IO; namespace ConsoleApplication1 { class Program { static void Main(string[] args) { binaryWriteRead(); //copy(); //Console.WriteLine("复制完成"); //txtRead(); } //二进制读写综合应用(适合搞啥子序列化啊游戏数据保存之类的) //注意本实例里的内存变化字节数等 static void binaryWriteRead() { //写入磁盘(在内存中是output 因为是输出到外部设备 ) //操作系统中stream是以内存为参照的 FileStream fs = File.Create("保存"); System.IO.BinaryWriter writer = new BinaryWriter(fs); Random rdm = new Random(); for (int i = 0; i < 10; i++)//int32 4字节X10 { int num = rdm.Next(); writer.Write(num); Console.WriteLine("写入整数:{0}", num); } char[] data = { 'h', 'e', 'l', 'l', 'o' }; writer.Write(data); Console.WriteLine("写入:hello"); writer.Write('-');//46字节 //写入26个字母的ascii码 //如果读取的时候使用的是streamReader 则读取的是对应ascii的字母 for (byte i = 97; i < 97 + 26; i++) writer.Write(i); Console.WriteLine("写入字母a~z"); writer.Write('\n');//71字节 Console.WriteLine("------------------------------------------"); fs.Flush(); fs.Close(); writer.Close(); //------------------------------------------------------------- //读取(在内存中是input) fs = File.OpenRead("保存"); BinaryReader reader = new BinaryReader(fs); //读取26个字母 fs.Seek(46, SeekOrigin.Begin); for (byte i = 97; i < 97 + 26; i++) Console.Write(reader.ReadChar()); //读取10个整数 fs.Seek(-72, SeekOrigin.Current); Console.WriteLine(); //int 代表 System.Int32 4个字节 //如果writer.Write(true) 实际上也是写入一个字节:0x0001 //字节是基本单位 byte[] numData = new byte[4]; for (int i = 0; i < 10; i++) { reader.Read(numData, 0, 4); Console.WriteLine("读取整数:{0}", byteArToLong(numData));//手工对byte数组进行转换 //Console.WriteLine("读取整数:{0}", reader.ReadInt32()); } reader.Close(); fs.Close(); } static int getNum(byte[] bytes)//二进制转十进制(我帖子里留言那位兄弟滴代码很牛x ^_^) { //long型数据类型为8字节 int num = 0; int n = Math.Min(bytes.Length, 4) - 1; for (int i = n; i > -1; i--) num = (num << 8) | bytes[i];//num = (num<<8) + bytes[i]; return num; } //字节数组转长整型(二进制转十进制) static long byteArToLong(byte[] bytes) { //int [] nums= {1,256,512,1024,2048,4096,8192,16384,32768,65536,131072,262144}; //当时我的代码是这样写的哈遇到大的数字就要出问题,一个字节8位应该是8的平方 而我写的确是2的平方 //不过上面那位留言的高手只顾自己开心了这么明显的错误也没看出来 //int srcData = 312705998; //比如上面的int值在内存中是0x12a383ce 的形式存储的 //但是他在文件中存储确是反过来的(低位在前 高位在后) //如果我一次读取4字节就是下面的形式 //byte []data = {0xce,0x83,0xa3,0x12}; int[] nums = { 1, 256, 65536, 16777216 }; if (bytes.Length > nums.Length) throw new Exception("溢出"); long num = 0; for (int i = 0; i < bytes.Length; i++) num += (bytes[i] * nums[i]); return num; } static void copy()//文件拷贝(二进制读写) { FileStream fs = File.OpenRead("d:/新建文件夹 (2).zip"); FileInfo copys = new FileInfo("d:/复件.zip"); FileStream fsCopy = copys.Create(); byte[] data = new byte[10]; int reds; while ((reds = fs.Read(data, 0, data.Length)) > 0) fsCopy.Write(data, 0, reds); fs.Flush(); fsCopy.Close(); fs.Close(); } static void txtRead()//文本读取 { //ANSI编码是个很蛋疼的东西 //比如utf-8还有其他编码存储的文本文件最开始会有两个字节用来识别编码的 //而ANSI没有 但是ANSI是经过扩了展的有汉字编码 //以前曾试过自己直接通过读取二进制再解码的方式来做 但是真滴很麻烦 //还是这样比较方便 偷懒 O(∩_∩)O哈哈~ FileStream fs = File.Open(@"d:/a.cs", FileMode.Open); StreamReader reader = new StreamReader(fs); string data; while ((data = reader.ReadLine()) != null) Console.WriteLine(data); } } }