经验总结29--C#,简单读写文件
2014-09-02 13:44
316 查看
写程序时,会经常进行文件的读写操作。
记录下,方便以后copy使用。
/// < ummary>
/// 写文件
/// </summary>
/// < aram name="Path">文件路径</param>
/// < aram name="Strings">文件内容</param>
public static void WriteFile(string Path, string Strings)
{
if (!System.IO.File.Exists(Path))
{
System.IO.FileStream f = System.IO.File.Create(Path);
f.Close();
}
System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.GetEncoding("gb2312"));
f2.WriteLine(Strings);
f2.Close();
f2.Dispose();
}
/// <summary>
/// 读文件
/// </summary>
/// <param name="Path">文件路径</param>
public static List<string> ReadFile(string Path)
{
List<string> result = new List<string>();
StreamReader txtStreamReader = new StreamReader(Path, System.Text.Encoding.GetEncoding("gb2312"));
while (txtStreamReader.Peek() != -1)
{
string strTxt = txtStreamReader.ReadLine().ToString();
if (strTxt.Length != 0)
{
result.Add(strTxt);
}
}
return result;
}
记录下,方便以后copy使用。
/// < ummary>
/// 写文件
/// </summary>
/// < aram name="Path">文件路径</param>
/// < aram name="Strings">文件内容</param>
public static void WriteFile(string Path, string Strings)
{
if (!System.IO.File.Exists(Path))
{
System.IO.FileStream f = System.IO.File.Create(Path);
f.Close();
}
System.IO.StreamWriter f2 = new System.IO.StreamWriter(Path, true, System.Text.Encoding.GetEncoding("gb2312"));
f2.WriteLine(Strings);
f2.Close();
f2.Dispose();
}
/// <summary>
/// 读文件
/// </summary>
/// <param name="Path">文件路径</param>
public static List<string> ReadFile(string Path)
{
List<string> result = new List<string>();
StreamReader txtStreamReader = new StreamReader(Path, System.Text.Encoding.GetEncoding("gb2312"));
while (txtStreamReader.Peek() != -1)
{
string strTxt = txtStreamReader.ReadLine().ToString();
if (strTxt.Length != 0)
{
result.Add(strTxt);
}
}
return result;
}
相关文章推荐
- WINCE应用层设计经验总结-关于INI文件读写的类
- ZYNQ系统中实现FAT32文件系统的SD卡读写之四 经验总结
- 对文件的简单读写操作
- java中调用SQLite的简单经验总结!
- Windows 文件过滤驱动经验总结
- 总结:内存映射文件与内存读写实现进程间通信的比较
- C/C++ 文件读写操作总结
- arcims开发经验总结(1) (aimsLayers.js 文件变量含义)
- arcims开发经验总结(1) (aimsLayers.js 文件变量含义)
- 使用Java实现对dbf文件的简单读写
- 使用Java实现对dbf文件的简单读写
- C/C++ 文件读写操作总结
- 经验总结系统文件无法打开故障解决方法
- 用c读写dbf文件的简单实现
- 【经验总结】关于文件复制的一个问题
- C++ builder 的文件读写操作总结
- 文件读写总结[keep updating]
- 关于论文参考文件的经验总结
- c#读写ANSI格式文件,解决中乱码问题最简单的写法
- 使用Java实现对dbf文件的简单读写