C#中文件读写方法很多,我这里主要说下我今天用到的方式 FileStream,这类文章网上也是一搜一大把,我也是搜出来的 - -
其实文件读写也主要就是 先来个文件流打开文件,然后操作(读写),最后释放资源,也就这样了
public class
FileHelper
{
public void WriteText(String path, String msg)
{
// 创建流,并打开文件
FileStream fs = new FileStream(path, FileMode.Append);
// 将要写入文件的字符串转为 Byte[],便于传入流中写入
// 其实对文件的操作,不管什么格式,读也就是把数据从流中存入 Byte[] 操作,写呢也就是把数据转为 byte[] 从流中写入
// 转为 byte[] 后,可用于传输,记得以前做了个C#版QQ,也就是这样实现聊天视屏传文件的,其实C#实现很简单,有兴趣的可以Google下
Byte[] data = Encoding.UTF8.GetBytes(msg);
// 写入 byte[] 数据
fs.Write(data, 0 , data.Length);
// 销毁流
fs.Flush();
fs.Close();
fs.Dispose();
}
// 一下就是一些调用时的重载了
public void Log(String msg)
{
String sysDrive = Environment.GetEnvironmentVariable("SystemDrive" );
String fileName = "BoocaaLog.txt" ;
String path = Path.Combine(sysDrive, fileName);
msg = DateTime.Now.ToString("yyyy/dd/MM HH:mm:ss") + " | " + msg;
msg = msg.Replace(Environment.NewLine, " " );
msg += Environment.NewLine;
this .WriteText(path, msg);
}
public void Log(String source, String line, String msg)
{
String sysDrive = Environment.GetEnvironmentVariable("SystemDrive" );
String fileName = "BoocaaLog.txt" ;
String path = Path.Combine(sysDrive, fileName);
msg = DateTime.Now.ToString("yyyy/dd/MM HH:mm:ss") + " | " + source + " | " + line + " | " + msg;
msg = msg.Replace(Environment.NewLine, " " );
msg += Environment.NewLine;
this .WriteText(path, msg);
}
public void Log(Exception ex)
{
String sysDrive = Environment.GetEnvironmentVariable("SystemDrive" );
String fileName = "BoocaaLog.txt" ;
String path = Path.Combine(sysDrive, fileName);
String msg = "" ;
msg = DateTime.Now.ToString("yyyy/dd/MM HH:mm:ss") + " | " + ex.Source + " | " + ex.TargetSite.ToString() + " | " + ex.Message + " | " + ex.StackTrace + msg;
msg = msg.Replace(Environment.NewLine, " " );
msg += Environment.NewLine;
this .WriteText(path, msg);
}
}
{
public void WriteText(String path, String msg)
{
// 创建流,并打开文件
FileStream fs = new FileStream(path, FileMode.Append);
// 将要写入文件的字符串转为 Byte[],便于传入流中写入
// 其实对文件的操作,不管什么格式,读也就是把数据从流中存入 Byte[] 操作,写呢也就是把数据转为 byte[] 从流中写入
// 转为 byte[] 后,可用于传输,记得以前做了个C#版QQ,也就是这样实现聊天视屏传文件的,其实C#实现很简单,有兴趣的可以Google下
Byte[] data = Encoding.UTF8.GetBytes(msg);
// 写入 byte[] 数据
fs.Write(data, 0 , data.Length);
// 销毁流
fs.Flush();
fs.Close();
fs.Dispose();
}
// 一下就是一些调用时的重载了
public void Log(String msg)
{
String sysDrive = Environment.GetEnvironmentVariable("SystemDrive" );
String fileName = "BoocaaLog.txt" ;
String path = Path.Combine(sysDrive, fileName);
msg = DateTime.Now.ToString("yyyy/dd/MM HH:mm:ss") + " | " + msg;
msg = msg.Replace(Environment.NewLine, " " );
msg += Environment.NewLine;
this .WriteText(path, msg);
}
public void Log(String source, String line, String msg)
{
String sysDrive = Environment.GetEnvironmentVariable("SystemDrive" );
String fileName = "BoocaaLog.txt" ;
String path = Path.Combine(sysDrive, fileName);
msg = DateTime.Now.ToString("yyyy/dd/MM HH:mm:ss") + " | " + source + " | " + line + " | " + msg;
msg = msg.Replace(Environment.NewLine, " " );
msg += Environment.NewLine;
this .WriteText(path, msg);
}
public void Log(Exception ex)
{
String sysDrive = Environment.GetEnvironmentVariable("SystemDrive" );
String fileName = "BoocaaLog.txt" ;
String path = Path.Combine(sysDrive, fileName);
String msg = "" ;
msg = DateTime.Now.ToString("yyyy/dd/MM HH:mm:ss") + " | " + ex.Source + " | " + ex.TargetSite.ToString() + " | " + ex.Message + " | " + ex.StackTrace + msg;
msg = msg.Replace(Environment.NewLine, " " );
msg += Environment.NewLine;
this .WriteText(path, msg);
}
}
我的用法呢,就是用他做一些程序日志的记录,比如在容易出错的地方加上记录,程序跑的时候有什么问题可查看日志快速定位问题点
下面贴一篇别人的帖子,他用到多种方式
http://www.cnblogs.com/kiddo/archive/2008/03/17/1110061.html
他在用到第二种方式时,也就是 StreamWriter 时代码