---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ---------------------- ----------------------
名称空间:System.IO
主要的类:FileStream 、 StreamWriter 、 StreamReader
FileStream对象表示在磁盘或网络路径上指向文件的流。提供了文件中读写字节(byte )的方法,其优势时可以用于任何数据的文件,包括图像和声音。需要通过几种转换类(Decoder 、 Encoder)的转换:
Decoder 解码:将字节序列转换为字符序列。
Encoder 编码:将字符序列转换为字节序列。
StreamWriter 、 StreamReader 都是面向字符、字符串。可以方便与FileStream对象的操作。
-----------------------------------------------------------------FileStream的操作------------------------------------------------------------------
构造函数:
FileStream afile=new FileStream(string path, FileMode,FileAccess)
参数:
①path:表示路径,可以使用OpenFileDialog获取路径。
②FileMode:打开文件的方式,枚举属性:
CreatNew:创建文件,若已存在,引发IOException异常。
Creat:创建文件,若已存在,则被覆盖。
Open:打开文件,若不存在,引发IOException异常。
OpenOrCreate:打开文件,若不存在,就建立。
Append:打开现有的文件,并查找到文件尾或创建新文件。
③FileAccess:控制对文件的读、写、读写访问:
Read:对文件的读访问。
Write:对文件的写访问。
ReadWrite:对文件的读写访问。
往文件流里写入数据:afile.Write(byte[] array,int offset, int count) :
array:数据来源,从哪里写入到文件里。
offset:从数据源的什么位置开始获取。
count:要从数据源拿多少字节。
byte[] byData;
char[] charData;
try
{
FileStream afile=new FileStream("Temp.txt",FileMode.Append); //创建FileStream并建立个txt文件
charData = "this is the first wenjian caozuo.".ToCharArray(); //采用字符串的.ToCharArray()转换为char数组 写入的数据
byData = new byte[charData.Length];
Encoder a = Encoding.UTF8.GetEncoder(); //Encoder 将字符转换为字节。
a.GetBytes(charData, 0, charData.Length, byData, 0, true); //转换成字节的方法
afile.Seek(0, SeekOrigin.Begin); //指明FileStream的开始位置
afile.Write(byData,0,byData.Length); //把字节数组写入到文件里
MessageBox.Show("文件写入成功!");
afile.Close(); //关闭留
}
catch(IOException ex)
{
MessageBox.Show("异常啊,亲:{0}",ex.ToString());
return;
}
从文件流里读取数据:afile.Read(byte[] array,int offset, int count)
byte[] byData=new byte[200];
char[] charData=new char[200];
try
{
FileStream fs = new FileStream("Temp.txt", FileMode.Open);
fs.Seek(0, SeekOrigin.Begin);
fs.Read(byData, 0, 200);
fs.Close();
}
catch (IOException ex)
{
MessageBox.Show("异常啊,亲:{0}", ex.ToString());
return;
}
// Decoder d = Encoding.UTF8.GetDecoder(); //将字节流转换为字符
// d.GetChars(byData, 0, byData.Length, charData, 0);
Encoding.UTF8.GetChars(byData, 0, byData.Length, charData, 0);
string s = new string(charData); //字符数组转换为字符串
MessageBox.Show(s);
-------------------------------------------------------------------------------StreamWriter-----------------------------------------------------------
StreamWriter:往文件流里写入数据,操作的对象的类型是字符、字符串主要方法:.Write() 和 .WriteLine()
try
{
StreamWriter swfile = new StreamWriter("Temp.txt",true); //声明个StreamWrite对象,并把文件的path传输给他,true表示已存在并附加到后面
//StreamWriter swfile = new StreamWriter(new FileStream fs); //也可以声明后,直接把FileStream对象传递给他,类似于SqlDataAdapter da=new SqlDataAdapter(SqlCommand)
MessageBox.Show("打开成功!");
swfile.WriteLine("第一次写的数据.");
swfile.Write("床前明月光,");
swfile.WriteLine("疑是地上霜。");
swfile.WriteLine("举头望明月,低头思故乡。");
swfile.Close();
}
catch(IOException ex)
{
MessageBox.Show("异常啊,亲:{0}", ex.ToString());
return;
}
MessageBox.Show("插入文字成功!");
-------------------------------------------------------------------------------StreamReader-----------------------------------------------------------
StreamReader:从文件流里读取数据。
主要的方法:
①Read():逐个字符的读取,读取的返回值为 int类型,要转换成字符 Convert.ToChar();
②ReadLine():一行一行的读取。
③ReadTOEnd():把文件里的数据一次性读取出来,文件太大的话内存方不下。
try
{
StreamReader sread = new StreamReader("Temp.txt"); //SreamReader跟StreamWriter很像
MessageBox.Show("打开成功!");
string s=sread.ReadLine(); //读取一行的数据
while(s!=null) //判断读取的数据
{
MessageBox.Show(s);
s = sread.ReadLine();
}
MessageBox.Show("读出完成!");
sread.Close();
}
catch (IOException ex)
{
MessageBox.Show("异常啊,亲:{0}", ex.ToString());
return;
}
try
{
StreamReader sread = new StreamReader("Temp.txt"); //SreamReader跟StreamWriter很像
MessageBox.Show("打开成功!");
int c = sread.Read(); //逐个字符的读取,返回为int类型
while (c != -1) //判断读取的数据
{
MessageBox.Show(Convert.ToChar(c).ToString());
c = sread.Read();
}
MessageBox.Show("读出完成!");
sread.Close();
}
catch (IOException ex)
{
MessageBox.Show("异常啊,亲:{0}", ex.ToString());
return;
}
------------------------------------------------------- 注意的事项------------------------------------------------------------------------------
1.文件流使用后要的关闭,调取 .Close()方法。
2.文件流容易发生异常,放到try...catch里。
---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ---------------------- ----------------------