这些天,我在学习中经常会遇到各种文件操作,比如读写文件、格式化、目录处理、序列化和解序对象等等。C#为我们提供了System.IO命名空间来包含文件操作的相关类,故来大致总结下。
1.File类与FileInfo类
Ø File类是静态的,可以在类上调用静态方法,而无需创建它们的实例;而FileInfo类仅可用于实例化的对象;
Ø 如果仅进行单一方法调用,则使用File 类的方法,它不必实例化新对象而直接调用
Ø 如果应用程序在文件上执行几种操作,则实例化FileInfo对象,这时对象已在文件系统上引用正确的文件,而静态类必须每次都寻找文件
Ø DirectoryInfo类与Directory类同上
2.流是序列化设备的抽象表示,序列化设备可以以线性方式存储数据,并可以以同样的方式访问:一次访问一个字节。把设备变成抽象的,就可以隐藏流的底层目标和源。这种抽象的级别支持代码的重用,而不必担心数据传输方式的特性。
3.上移目录,使用'../'的形式
4.FileStream对象表示在磁盘或网络路径上指向文件的流,但经常使用StreamReader或StreamWriter执行这些功能,因为FileStream类操作的是字节和字节数组,而Stream类操作的是字符数据。
6.因为FileStream类只能处理原始字节,故可以利用其处理任何数据文件,比如读取图像和声音文件。
-
FileStream aFile = new FileStream(filename, FileMode.Open);
-
aFile.Seek(135, SeekOrigin.Begin);
-
aFile.Read(byData, 0, 100);
5. FileStream操作字节和字节数组的优势在于,可以随机访问文件中某点的数据 (seek方法),对于获取的字节数组,利用System.Text.Decoder类将其转换为字符数组;反之,也可以利用其解码。
-
Decoder d = Encoding.UTF8.GetDecoder();
-
d.GetChars(byData, 0, byData.Length, charData, 0);
-
-
6.实例:处理csv文件
7.读写压缩文件(GZIP或Deflate算法)
-
FileStream fileStream = new FileStream(filename, mode);
-
GZipStream compressionStream = new GZipStream(fileStream, mode);
-
-
StreamWriter writer = new StreamWriter(compressionStream);
-
writer.Write(data);
8.应用程序有时候需要以对象的形式存储数据,.NET Framework中的System.Runtime.Serialization命名空间提供了序列化对象的基础架构。
-
IFormatter serializer = new BinaryFormatter();
-
serializer.Serialize(mystream,myObject);
MyObjectType myNewObject=serializer.Deserialize(myStream) as MyObjectType;
9..NET Framework 要求把对象标记为可序列化[Serializable],才能序列化它们,原因:
Ø 一些对象序列化的效果不好
Ø 一些对象包含敏感数据,这些数据不应以不安全的方式保存或传送到另一个进程中。
如果一个对象的成员包含敏感数据,可将这个字段或属性标记为[NonSerialized]
10.举一例经常会遇见的,操作逗号分隔符文件
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)