标签:
//Stream是一个抽象类,不能直接实例化
//FileStream继承于Stream ,读写文件流
//StreamWriter继承于TextWriter(抽象类,继承于MarshalByRefObject, IDisposable),可以操作Stream
//StreamReader继承于TextReader(抽象类,继承于MarshalByRefObject, IDisposable),可以操作Stream
//MemoryStream继承于Stream
//BufferedStream继承于Stream
StreamWriter和StreamReader可以直接操作文件进行读写,但前提是文件是已经存在的,如果没有文件存在,会抛出异常。
//文件流操作
private void btnFileStream_Click(object sender, EventArgs e)
{ string path=@"c:\test.txt";
#region FileStream 读写文件必须以字节形式
//向文件中写入流
using (FileStream fs = File.Create(path))
{
//byte[] info = new UTF8Encoding(true).GetBytes("this is some text");
Byte[] info = Encoding.GetEncoding("UTF-8").GetBytes("this is some text");
fs.Write(info, 0, info.Length);
}
//打开文件读回流
using (FileStream fs = File.OpenRead(path))
{
string result = string.Empty;
byte[] info = new byte[1024];
//UTF8Encoding temp = new UTF8Encoding(true);
while (fs.Read(info, 0, info.Length) > 0)
{
//result += temp.GetString(info);
result += Encoding.GetEncoding("UTF-8").GetString(info);
}
System.Diagnostics.Debug.WriteLine(result);
}
#endregion
#region StreamWriter 以字符的形式写入 1.可以往Stream里写,2.也可以直接往路径文件里写信息(前提是文件已存在)
//打开文件,在文件末尾附加信息
FileStream fileStream = new FileStream(path,FileMode.Append);
StreamWriter sw = new StreamWriter(fileStream);
sw.WriteLine("This is the appended line.");
sw.Close();
fileStream.Close();
//打开文件,在文件末尾附加信息
using (FileStream file = new FileStream(path, FileMode.Append))
{
using (StreamWriter writer = new StreamWriter(file))
//using (StreamWriter writer = new StreamWriter(path))
{
writer.WriteLine("this a the second appended line.");
writer.Write("this a the third appended line.");
}
}
//如果文件已经存在,可以及直接用StreamWriter写入,不用File
//using (StreamWriter writer = new StreamWriter(path))
// {
// writer.WriteLine("this a the second appended line.");
// writer.Write("this a the third appended line.");
// }
#endregion
#region StreamReader 1.可以从Stream里读,2.也可以直接从文件里读信息
//读取文件文本内容 1
using (StreamReader reader = new StreamReader(path,Encoding.UTF8))
{
string line;
while ((line = reader.ReadLine()) != null)
{
System.Diagnostics.Debug.WriteLine(line);
}
}
//读取文件文本内容 2
using(FileStream fs1=File.OpenRead(path))
{
using (StreamReader reader = new StreamReader(fs1,Encoding.GetEncoding("GB2312")))
{
string result = reader.ReadToEnd();
System.Diagnostics.Debug.WriteLine(result);
}
}
#endregion
#region 内存流,可读可写,操作单位是字节,主要作用是在内存中操作字节流,最后可写入到Stream中
using (MemoryStream ms = new MemoryStream())
{
UnicodeEncoding uniEncoding = new UnicodeEncoding();
byte[] info = uniEncoding.GetBytes(" this is MemoryStream");
//把信息写入到MemoryStream中
ms.Write(info, 0, info.Length);
byte[] result=new byte[ms.Length];
//从流中读取信息到字节数组中
ms.Read(result, 0, info.Length);
using (Stream s = new FileStream("c:/aa.txt",FileMode.Create))
{
//可以将内存流写入到Stream中:this is MemoryStream
ms.WriteTo(s);
}
}
#endregion
//缓冲区是内存中的字节块,用于缓存数据,从而减少对操作系统的调用次数。 缓冲区可提高读取和写入性能。
//BufferedStream
}
写日志:
SaveFileDialog saveFileDialog = new SaveFileDialog();
saveFileDialog.Filter = "log files(*.log)|*.log|txt files(*.txt)|*.txt|All files(*.*)|*.*";
saveFileDialog.FilterIndex = 0;
saveFileDialog.RestoreDirectory = true;
saveFileDialog.FileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".log";
FileStream fs = null;
StreamWriter sw = null;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
try
{
fs = (FileStream)saveFileDialog.OpenFile();
sw = new StreamWriter(fs);
sw.Write(txtImportLog.Text);
}
catch (Exception ex)
{
MessageBox.Show("日志导出失败!" + ex.Message);
}
finally
{
sw.Flush();
sw.Close();
fs.Close();
}
}
标签:
原文地址:http://www.cnblogs.com/experience/p/4494835.html