.Net高级技术:IO处理深入

一、文件的读写操作

1.File,Directory,Path三个静态类的静态方法实现拷贝指定路径的指定格式文件:针对的是小文件的读写,当涉及大文件的时候还是要使用流来读写

//将制定目录下匹配的文件全部Copy出来
string sourcePath = "D:\\Projects\\";
string desPath=Directory.GetCurrentDirectory()+"\\Data\\";
string[] files= Directory.GetFiles(sourcePath, "*.exe", SearchOption.AllDirectories);
if (!Directory.Exists(desPath))
{
Directory.CreateDirectory(desPath);
}
foreach (var file in files)
{
string desFile = desPath + Path.GetFileName(file);//处理文件被占用不能拷贝的情况写入到日志文件中
File.Copy(file, desFile, true);//将匹配的文件拷贝到当前程序所在的路径
}

 

2.FileStream 大文件的分步拷贝:所有的文本文件或者非文本文件早磁盘或内存的存在都是字节  

string sourcePath = @"D:\Movie\xiawu2-树状结构处理.avi";
string desPath = @"D:\Movie\a.avi";
using (FileStream outStream=new FileStream(desPath,FileMode.OpenOrCreate))
{
using (FileStream inputStream=new FileStream(sourcePath,FileMode.Open))
{
byte[] arry=new byte[1024*1024*4];//设置缓冲区的大小为4M,即每次写入4M的字节
int readLength=0;//记录每次读入内存的实际字节数
while ((readLength = inputStream.Read(arry, 0, arry.Length))>0)//不停的往后读
{
outStream.Write(arry, 0, readLength);//每次写入实际的字节长度
}
}
}

备注:上述的过程可以对字节进行处理实现视频加密,按照加密的逆向实现视频的解密

 

3.File.OpenRead(),File.OpenWrite是对FileStream的简化操作,直接返回一个Stream的对象

 

4.StreamReader和StreamWriter是针对于文本的读写,操作起来更加简单,由于是要处理字节流和stream的转化关系,所以这个地方最好是指定编码格式

 

5.使用webClient类从网络上读取Excel文件的内容,首先读取一个网络流,使用NPOI将流中的文件读取并解析。

   但是这里有一个问题,网络流是不支持指针后退的,NPOI需要指针可以后退的流来操作,所以需要将网络流转为内存流(将网络流读入到内存流中),再交给NPOI处理

 

6.读写文件.NET托管环境无法完成,需要通过.NET通知操作系统来写入,资源不会被QC维护,所以它有dispose方法来释放资源

 

二、对象的序列化

程式运行过程中对象存储在内存中,关闭程式想要再次使用对象已经不可能,这个时候可以说会用BinaryFormatter来进行序列化和反序列化

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

BinaryFormatter bf = new BinaryFormatter();

private void button1_Click(object sender, EventArgs e)
{
var p = new Person() { Name="Tom",Age=20};

using(Stream stream = File.OpenWrite(@"D:\Movie\1.dat"))
{
//将一个对象序列化到一个写入的文件流中保存起来
bf.Serialize(stream, p);
}
MessageBox.Show("序列化成功!");
}

private void button2_Click(object sender, EventArgs e)
{
using (Stream stream = File.OpenRead(@"D:\Movie\1.dat"))
{
Person p=bf.Deserialize(stream) as Person;
MessageBox.Show(p.Name+":"+p.Age);
}
}
}

[Serializable]
public class Person
{
public string Name { set; get; }
public int Age { set; get; }
}

 

三、单例模式

//设置单例

public partial class Form2 : Form
{
//提供公有的静态获取单利对象入口
private static Form2 frm;

public static Form2 GetSingleton()
{
if (frm==null||frm.IsDisposed)//对象为NULL或者被释放
{
frm = new Form2();
}
return frm;
}

//私有化构造函数
private Form2()
{
InitializeComponent();
}
}

 

//使用单例

public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
Form2 frm = Form2.GetSingleton();
frm.Show();
}
}

注释:以上的方法在多线程的时候会出现问题:

最通用的实现单例方法:(不需要懒汉式等其他复杂的实现方法)

public partial class Form2 : Form
{
//提供公有的静态获取单利对象入口
private static Form2 frm=new Form2();//只在类初始化的时候创建一次,解决多线程的问题

public static Form2 GetSingleton()
{
return frm;
}

//私有化构造函数
private Form2()
{
InitializeComponent();
}
}

posted @ 2019-01-04 14:56  Francis_Ray  阅读(217)  评论(0编辑  收藏  举报