IO操作(C#)
在一些游戏当中 , 通常将一些参数写在配置里面, 无论是XML , EXCEL , TXT , BIN …… 我相信都会用到其中的一种 。首先 , XML , BIN 对于策划人员来说就很容易配置错误了。如果能用程序将Excel里面的数据转化为Txt , Bin , Xml数据, 就很爽了 , 关于Excel数据的读写我在前几篇博文中写过。 这里就涉及到了I/O的操作 。废话不多说 , 上代码 . 这里写的都是同步I/O操作 。
usingSystem;usingSystem.Collections;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.IO;namespaceComprehensiveTest.com.myio{publicclassIoManager{privatestaticIoManagerinstance=null;publicstaticIoManagerInstance{get{if(IoManager.instance==null){IoManager.instance=newIoManager();}returnIoManager.instance;}}///<summary>//////</summary>///<paramname="targetPath"></param>///<returns></returns>publicboolCreateFile(stringtargetPath){if(File.Exists(targetPath)){returntrue;}else{try{//使用这2种方法都可以//FileStreamfile=File.Create(targetPath);FileStreamfile=newFileStream(targetPath,FileMode.Create);file.Close();returntrue;}catch(Exceptione){Console.WriteLine("创建文件{0},失败,原因:{1}",targetPath,e.ToString());returnfalse;}}}///<summary>///获得电脑所有的驱动盘///</summary>///<returns></returns>publicstring[]GetMyLogicalDrives(){returnDirectory.GetLogicalDrives();}///<summary>///移动数据///</summary>///<paramname="oldPath">原始的路径</param>///<paramname="newPath">新的路径</param>///<returns>操作是否成功</returns>publicboolMoveFile(stringoldPath,stringnewPath){if(File.Exists(oldPath)){try{File.Move(oldPath,newPath);returntrue;}catch(Exceptione){Console.WriteLine("移动文件{0},失败,原因:{1}",oldPath,e.ToString());returnfalse;}}else{Console.WriteLine("Error,{0}文件不存在!!!",oldPath);returnfalse;}}///<summary>///复制一个文件///</summary>///<paramname="oldPath"></param>///<paramname="newPath"></param>///<returns></returns>publicboolCopyFile(stringoldPath,stringnewPath){if(File.Exists(oldPath)){try{File.Copy(oldPath,newPath);returntrue;}catch(Exceptione){Console.WriteLine("复制文件{0},失败,原因:{1}",oldPath,e.ToString());returnfalse;}}else{Console.WriteLine("Error,{0}文件不存在!!!",oldPath);returnfalse;}}///<summary>///删除一个文件///</summary>///<paramname="targetPath"></param>///<returns></returns>publicboolDeleteFile(stringtargetPath){if(File.Exists(targetPath)){try{File.Delete(targetPath);returntrue;}catch(Exceptione){Console.WriteLine("删除文件{0},失败,原因:{1}",targetPath,e.ToString());returnfalse;}}else{Console.WriteLine("Error,{0}文件不存在!!!",targetPath);returnfalse;}}///<summary>///创建一个文件夹///</summary>///<paramname="path"></param>///<returns></returns>publicboolCreateFolder(stringpath){if(Directory.Exists(path)){Console.WriteLine("文件夹{0}已经存在",path);returntrue;}else{try{DirectoryInfodirInfo=Directory.CreateDirectory(path);Console.WriteLine("创建文件夹成功,创建时间为{0}",Directory.GetCreationTime(path));returntrue;}catch(Exceptione){Console.WriteLine("创建文件夹失败,失败原因{0}",e.ToString());returnfalse;}}}///<summary>///删除文件夹///</summary>///<paramname="path"></param>///<returns></returns>publicboolDeleteFolder(stringpath){if(Directory.Exists(path)){try{Directory.Delete(path);returntrue;}catch(Exceptione){Console.WriteLine("删除文件夹失败,失败原因{0}",e.ToString());returnfalse;}}else{returntrue;}}///<summary>//////</summary>///<paramname="oldPath"></param>///<paramname="newPath"></param>///<returns></returns>publicboolMoveFolder(stringoldPath,stringnewPath){if(Directory.Exists(oldPath)){try{Directory.Move(oldPath,newPath);returntrue;}catch(Exceptione){Console.WriteLine("移动文件夹{0},失败,原因:{1}",oldPath,e.ToString());returnfalse;}}else{Console.WriteLine("Error,{0}文件夹不存在!!!",oldPath);returnfalse;}}///<summary>///读取文件(一个个读)老是在流以外,无法读到正确的值///</summary>///<paramname="targetPath"></param>///<returns></returns>publicboolReadOneByOneTest(stringtargetPath){if(File.Exists(targetPath)){FileStreamfs=newFileStream(targetPath,FileMode.Open,FileAccess.Read);BinaryReaderbr=newBinaryReader(fs);br.BaseStream.Seek(0,SeekOrigin.Begin);//将指针设到开头while(br.BaseStream.Position<br.BaseStream.Length){try{Console.WriteLine(br.ReadString());}catch(EndOfStreamExceptione){Console.WriteLine("已经到了结尾{0}",e.ToString());}}br.Close();fs.Close();returntrue;}else{returnfalse;}}///<summary>///读取文本///</summary>///<paramname="targetPath"></param>///<returns></returns>publicboolReadCommon(stringtargetPath){if(File.Exists(targetPath)){//using(StreamReadersr=File.OpenText(targetPath))//读中文将乱码using(StreamReadersr=newStreamReader(targetPath,UnicodeEncoding.GetEncoding("GB2312")))//解决中文乱码问题{stringreadStr;while((readStr=sr.ReadLine())!=null){Console.WriteLine(readStr);}sr.Close();}returntrue;}else{returnfalse;}}///<summary>//////</summary>///<paramname="targetPath"></param>///<paramname="content"></param>///<paramname="isNendWarp"></param>///<returns></returns>publicboolWriteCommon(stringtargetPath,stringcontent,boolisNendWarp){if(File.Exists(targetPath)){//using(StreamWritersw=File.AppendText(targetPath))//中文乱码using(StreamWritersw=newStreamWriter(targetPath,true,UnicodeEncoding.GetEncoding("GB2312")))//解决中文乱码问题{if(isNendWarp){sw.WriteLine(content);}else{sw.Write(content);}sw.Close();}returntrue;}else{returnfalse;}}}}
StreamWriter(targetPath,true,UnicodeEncoding.GetEncoding("GB2312"))
第二个参数为false : 表示覆盖写入
这里我都测过了 , OK的 , 有一个方法 : ReadOneByOneTest 老是读不到数据 :
望高手指点!
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。