C#读写文件
2015-08-17 16:29
543 查看
//下面是读写文件的类 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; namespace WriteRead { class FileC { #region Write TO File . Params:path and content public static void writeFileStream(string path,string content) { byte [] date=System.Text.Encoding.Default.GetBytes(content); try { FileStream file = new FileStream(path, FileMode.Append); file.Write(date, 0, date.Length); file.Flush(); file.Close(); } catch (Exception) { throw; } } /*先使用fileStream创建文件,然后使用StreanWrite 写进文件 * * */ public static void writeStreamWrite(string path, string content) { try { FileStream file = new FileStream(path, FileMode.Append); //创建一个StreamWrite 实例进行输入 StreamWriter sw = new StreamWriter(file); //写入文件 sw.Write(content); //关闭缓冲区。把缓冲区中的数据写入文件 sw.Flush(); sw.Close(); file.Close(); } catch (Exception) { throw; } } /* * 使用Bianary写文件 * 以二进制的形式写文件 */ public static void writeBinaryWriter(string path, string content) { try { FileStream file = new FileStream(path,FileMode.Append); BinaryWriter bw = new BinaryWriter(file); bw.Write(content); bw.Flush(); bw.Close(); } catch (Exception) { throw; } } #endregion #region read from file params:path /* * 使用fileStream读取文件return byte[] */ public static byte[] readFile(string path) { byte[] data; try { //FileStream 构造函数有14个重载,其中常用的就是这个,下面这个还可以读写的权限。 // FileStream file1=new FileStream (path, FileMode.Open,FileAccess.ReadWrite) FileStream file = new FileStream(path, FileMode.Open); data = new byte[file.Length]; file.Read(data, 0, data.Length); file.Close(); return data; } catch (Exception) { return null; throw; } } /* * 以字节流的方式读文件 * 使用StreamReader读取文件return byte[] */ public static string readStreamReader(string path) { string data=string.Empty; try { FileStream file = new FileStream(path, FileMode.Open); file.Seek(0, SeekOrigin.Begin); StreamReader sr = new StreamReader(file); string strPrem=string.Empty; while ((strPrem=sr.ReadLine())!=null) { data += strPrem; } sr.Close(); file.Close(); return data; } catch (Exception) { return null; throw; } } /* * 以二进制的方式读文件 * 使用BinaryReader读取文件return byte[] */ public static byte[] readBinaryReader(string path) { try { FileStream file = new FileStream(path, FileMode.Open); file.Seek(0, SeekOrigin.Begin); //一般使用二进制方式读文件,因为不受限制,是什么类型的文件都可以读进来 BinaryReader br = new BinaryReader(file); byte [] data=new byte [file.Length]; br.Read(data, 0, data.Length); br.Close(); file.Close(); return data; } catch (Exception) { return null; throw; } } #endregion } }
下面是测试类的主程序:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace WriteRead { class Program { static void Main(string[] args) { //TestFileStreamWrite(); //TestStreamWriter(); //TestBinaryWriter(); // Console.WriteLine("write success!"); // Testread(); // TestStreamReader(); //TestBinaryReader(); Console.ReadKey(); } #region Test read from file public static void Testread() { string strPath = "E:\\t.txt"; byte[] data = FileC.readFile(strPath); String strcontent = System.Text.Encoding.Default.GetString(data); Console.WriteLine(strcontent); } public static void TestStreamReader() { string strPath = "E:\\t.txt"; Console.WriteLine(FileC.readStreamReader(strPath)); } public static void TestBinaryReader() { string strPath = "E:\\t.txt"; byte[] data = FileC.readBinaryReader(strPath); String strcontent = System.Text.Encoding.Default.GetString(data); Console.WriteLine(strcontent); Console.WriteLine(); } #endregion #region Test Write Class public static void TestFileStreamWrite() { string strContent = "This is a TXT document"; string strPath="E:\\t.txt"; FileC.writeFileStream(strPath, strContent); } public static void TestStreamWriter() { string strContent = "This is a TestStreamWriter document"; string strPath = "E:\\t.txt"; FileC.writeStreamWrite(strPath, strContent); } public static void TestBinaryWriter() { string strContent = "This is a TestBinaryWriter document"; string strPath = "E:\\t.txt"; FileC.writeBinaryWriter(strPath, strContent); } #endregion } }
相关文章推荐
- c# 获取路径的几种方法
- 可视化对比十多种排序算法(C#版)
- C# 系统应用之TreeView控件 (一).显示树状磁盘文件目录及加载图标(转)
- C# 中的IOCP线程池
- C# 中的IOCP线程池
- 编写高质量代码改善C#程序的157个建议——建议58:用抛出异常代替返回错误代码
- C# String与string的区别 以及 C# Swap string
- C#应用程序只允许运行一个实例,多次运行的时候激活当前实例,显示其界面
- ChartServer
- C# 模仿QQ右下角 消息闪烁提示
- C#
- 转载C#中Trim()、TrimStart()、TrimEnd()的用法
- C# Dictionary增加的方法
- C#判断一天、一年已经过了百分之多少的方法
- C#实现将程序运行信息写入日志的方法
- C# WPF设置窗口透明
- C#实现的简单链表类实例
- c#写入日期信息到excel中
- 问题:C#打开一个文本文档往里面写数据,没有就新建文档 ;结果:c#FileStream文件读写(转)
- C#直线的最小二乘法线性回归运算实例