The following is a class using system;using system.collections.generic;using system.linq;using that reads and writes files 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; }/* First create the file using FileStream and then write the file using Streanwrite * */public static void Writestreamwrite (string path, string content) {try {FileStream file = n EW FileStream (path, filemode.append); Create a StreamwriTe instance is input StreamWriter sw = new StreamWriter (file); Write to file SW. Write (content); Closes the buffer. Writes the data in the buffer to the file SW. Flush (); Sw. Close (); File. Close (); } catch (Exception) {throw; }/* * Use bianary Write file * Write file in binary form */public static void Writebinarywriter (ST Ring path, string content) {try {FileStream file = new FileStream (Path,filem Ode. Append); BinaryWriter bw = new BinaryWriter (file); Bw. Write (content); Bw. Flush (); Bw. Close (); } catch (Exception) {throw; }} #endregion #region read from file params:path/* * reads files with FileStream return byte[] */ public static byte[] ReaDfile (string path) {byte[] data; The try {//filestream constructor has 14 overloads, which is what is commonly used, and the following permission to read and write. FileStream file1=new FileStream (Path, filemode.open,fileaccess.readwrite) FileStream file = new Filestr EAM (path, FileMode.Open); data = new Byte[file. Length]; File. Read (data, 0, data. Length); File. Close (); return data; } catch (Exception) {return null; Throw }/* * Read file in byte stream * Use StreamReader to read file 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 }/* * Read the file in binary mode * Use BinaryReader to read the file return byte[] */public static byte[] Readbinaryreader (string path) {try {FileStream file = new FileStream (path, FileMode.Open); File. Seek (0, Seekorigin.begin); Generally use binary mode to read the file, because unrestricted, what type of file can be read in 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}}
Here is the main program for the test class:
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) {//testfilestreamwrit E (); Teststreamwriter (); Testbinarywriter (); Console.WriteLine ("Write success! "); Testread (); Teststreamreader (); Testbinaryreader (); Console.readkey (); } #region Test read from the 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 () {s Tring 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 documen T "; String strpath = "E:\\t.txt"; Filec.writestreamwrite (strpath, strcontent); } public static void Testbinarywriter () {String strcontent = ' This is a testbinarywriter documen T "; String strpath = "E:\\t.txt"; Filec.writebinarywriter (strpath, strcontent); } #endregion}}
Copyright statement: Personal wealth, want to learn, I teach you ah ....
C # Read and write files