标题: 创建和读写文件的一些简单方法 [打印本页] 作者: 来明坤 时间: 2012-8-28 01:32 标题: 创建和读写文件的一些简单方法 1 使用StreamWriter向文件写入数据
[c-sharp] view plaincopyprint?
public static void WriteToLog(string logFileName, string data)
{
using (FileStream fileStream = new FileStream(logFileName, FileMode.Append, FileAccess.Write, FileShare.None))
{
using (StreamWriter streamWrtie = new StreamWriter(fileStream))
{
streamWrtie.Write(data);
}
}
}
2 用StreamReader读取文本
[c-sharp] view plaincopyprint?
public static string ReadAllLog(string logFileName)
{
if (!File.Exists(logFileName))
{
throw (new FileNotFoundException("logfile does not exit", logFileName));
}
string contents = "";
using (FileStream fileStream = new FileStream(logFileName, FileMode.Open, FileAccess.Read, FileShare.None))
{
using (StreamReader streamRead = new StreamReader(fileStream))
{
contents = streamRead.ReadToEnd();
}
}
return contents;
}
3 要逐行的读取文本,则使用peek()方法
[c-sharp] view plaincopyprint?
public static void ReadLogPeeking(string logFileName)
{
if (!File.Exists(logFileName))
{
throw (new FileNotFoundException("logfile does not exit", logFileName));
}
using (FileStream fileStream = new FileStream(logFileName, FileMode.Open, FileAccess.Read, FileShare.None))
{
using (StreamReader streamReader = new StreamReader(fileStream))
{
while (streamReader.Peek() != -1)
{
Console.WriteLine(streamReader.ReadLine());
}
}
}
}
或
[c-sharp] view plaincopyprint?
public static void ReadLogByLines(string logFileName)
{
if (!File.Exists(logFileName))
{
throw (new FileNotFoundException("logfile does not exit", logFileName));
}
using (FileStream fileStream = new FileStream(logFileName, FileMode.Open, FileAccess.Read, FileShare.None))
{
using (StreamReader streamReader = new StreamReader(fileStream))
{
string text = streamReader.ReadLine();
while (text != null)
{
Console.WriteLine(text);
text = streamReader.ReadLine();
}
}
}
}
4 若要读取一个字节则用read()方法
[c-sharp] view plaincopyprint?
public static void ReadLogAsBytes(string logFileName)
{
if (!File.Exists(logFileName))
{
throw (new FileNotFoundException("logfile does not exit", logFileName));
}
using (FileStream fileStream = new FileStream(logFileName, FileMode.Open, FileAccess.Read, FileShare.None))
{
using (StreamReader streamReader = new StreamReader(fileStream))
{
while (streamReader.Peek() != -1)
{
Console.WriteLine(streamReader.Read());
}
}
}
}
5 按块读取,则要创建一个任意长度的缓冲区,这个长度根据你的性能需求来决定
[c-sharp] view plaincopyprint?
public static void ReadAllBuffereLog(string logFileName)
{
if (!File.Exists(logFileName))
{
throw (new FileNotFoundException("logfile does not exit", logFileName));
}
using (FileStream fileStream = new FileStream(logFileName, FileMode.Open, FileAccess.Read, FileShare.None))
{
using (StreamReader streamReader = new StreamReader(fileStream))
{
while (streamReader.Peek() != -1)
{
char[] buffer=new char [10];
int bufferFileSize = streamReader.Read(buffer, 0, 10);
foreach (char c in buffer)
{
Console.Write(c);
}
Console.WriteLine();
Console.WriteLine(bufferFileSize);
}
}
}
} 作者: 资建文 时间: 2012-8-28 08:40
恩 学习学习