A, FileStream file stream
1. Read Data
. 1 public class the ReadFile 2 { . 3 /// <Summary> . 4 /// read files . 5 /// FileMode.Create create a new file, if the file already exists overwrite the old file . 6 /// FileMode.CreateNew create a file If the file exists will be abnormal, suggesting that the file already exists 7 /// FileMode.Open open a file, if the file does not exist, an exception 8 /// FileMode.OpenOrCreate open a file, if the file does not exist, a new file is created and open the file . 9 /// FileMode.APPEND open an existing file, and the file be appended to the existing content, the file does not exist if the abnormality 10 /// FileMode.Truncate the existing operating system, the file contents inside taken, if the file absence of the abnormality . 11 /// </ Summary> 12 is public static void Read(string FilePath) 13 { 14 FileStream fileStream = null; 15 try 16 { 17 fileStream = new FileStream(FilePath, FileMode.Truncate); 18 byte[] bytes = new byte[fileStream.Length]; 19 int read = fileStream.Read(bytes, 0, bytes.Length); 20 var result = Encoding.UTF8.GetString(bytes); 21 } 22 catch (Exception e) 23 { 24 if (fileStream != null) 25 { 26 fileStream.Dispose(); 27 } 28 Console.WriteLine(e.Message); 29 } 30 finally 31 { 32 if (fileStream != null) 33 { 34 fileStream.Close(); 35 fileStream.Dispose(); 36 } 37 } 38 } 39 }
2. Write data
. 1 public class the WriteFile 2 { . 3 public static void the WriteText ( String FilePath, String writeString,) . 4 { . 5 the FileStream fileStream = null ; . 6 the try . 7 { . 8 // The path to open the file . 9 fileStream = new new the FileStream ( @ " C: \ the Users \ Administrator \ Source \ Repos \ OperatFile \ OperatFile \ 1.txt " , FileMode.APPEND); 10 // string converted to byte 11 byte[] bytes = Encoding.UTF8.GetBytes(writeString); 12 //写入到文件 13 fileStream.Write(bytes, 0, bytes.Length); 14 } 15 catch (Exception e) 16 { 17 if (fileStream != null) 18 { 19 fileStream.Dispose(); 20 } 21 Console.WriteLine(e.Message); 22 } 23 finally 24 { 25 // close and release 26 is IF (fileStream =! Null ) 27 { 28 fileStream.Close (); 29 fileStream.Dispose (); 30 } 31 is } 32 } 33 is }
Two, StreamReader text flow
1. Read Data
1 public class SteamReadFile 2 { 3 /// <summary> 4 /// 读取文件 5 /// </summary> 6 /// <param name="filePath">文件路径</param> 7 public static void ReadFile(string FilePath) 8 { 9 try 10 { 11 using (StreamReader sr = new StreamReader(FilePath)) 12 { 13 var result = sr.ReadToEnd(); 14 Console.WriteLine(result); 15 } 16 } 17 catch (Exception e) 18 { 19 20 throw new Exception(e.Message); 21 } 22 } 23 }
2. Write data
. 1 public class StreamWriteFile 2 { . 3 /// <Summary> . 4 /// written to the file . 5 /// </ Summary> . 6 /// <param name = "FilePath"> File Path </ param> . 7 /// <param name = "WriteString"> string to be written </ param> . 8 public static void the WriteFile ( string FilePath, string WriteString) . 9 { 10 the try . 11 { 12 is the using (SR = the StreamWriternew StreamWriter(FilePath)) 13 { 14 sr.WriteLine(WriteString); 15 } 16 } 17 catch (Exception e) 18 { 19 throw new Exception(e.Message); 20 } 21 } 22 }
3. Write log instance
. 1 public class LogHelper 2 { . 3 /// <Summary> . 4 /// file path . 5 /// </ Summary> . 6 public static String FilePath = @ " C: \ the Users \ Administrator \ Source \ Repos \ OperatFile \ OperatFile \ files " ; . 7 static LogHelper () . 8 { . 9 // determines folder exists, if not, the re-create the 10 IF (! Directory.Exists (FilePath)) . 11 { 12 is Directory.CreateDirectory (FilePath); 13 is } 14 } 15 /// <Summary> 16 /// log write . 17 /// Path.Combine (str1, str2, Str3) stitching together the passed parameters, and returns the new string 18 /// File.AppendText (fullPath) according to the file path, the writing of the new contents, the text that follows the splice . 19 /// </ Summary> 20 is public static void WriteLog () 21 is { 22 is the try 23 is { 24 var SB = BindData (); 25 String fullPath = Path.Combine (FilePath, $ " {DateTime.Now.ToString ( "the MM-dd-YYYY " .)} TXT " ); 26 is // determine whether the file exists, if not, then the new file 27 IF (! the File.Exists (fullPath)) 28 { 29 File.Create (fullPath); 30 } 31 is the using (SW = the StreamWriter File.AppendText (fullPath)) 32 { 33 is sw.WriteLine (sb.ToString ()); 34 is } 35 } 36 the catch (Exception E) 37 [ { 38 is the throw new new Exception (e.Message); 39 } 40 41 is } 42 is /// <Summary> 43 is /// binding log information 44 is /// </ Summary> 45 /// <Returns> </ Returns> 46 is Private static BindData the StringBuilder () 47 { 48 the StringBuilder SB = new new the StringBuilder (); 49 the DateTime = operatDateTime the DateTime.Now; 50 String Content = " file read and write functions " ; 51 is String= Operators " Bob " ; 52 is sb.AppendLine ($ " operation time: operatDateTime {} " ); 53 is sb.AppendLine ($ " Operation Content: Content {} " ); 54 is sb.AppendLine ($ " operator: {operators } " ); 55 sb.AppendLine ( " ---------------------------------------- -------------------------------------------------- " ); 56 is return SB; 57 is } 58 }
Three, MemoryStream memory stream
1 /// <summary> 2 /// 根据URL读取内容到内存流 3 /// </summary> 4 /// <param name="url"></param> 5 /// <returns></returns> 6 public static string DownLoadByUrl(string url) 7 { 8 string result = string.Empty; 9 MemoryStream ms = null; 10 HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest; 11 HttpWebResponse response = request.GetResponse() as HttpWebResponse; 12 using (var stream = response.GetResponseStream()) 13 { 14 byte[] buffer = new byte[response.ContentLength]; 15 int actuallyRead = 0, offset = 0; 16 do 17 { 18 actuallyRead = stream.Read(buffer, offset, buffer.Length - offset); 19 offset += actuallyRead; 20 21 } while (actuallyRead > 0); 22 ms = new MemoryStream(buffer); 23 ms.Seek(0, SeekOrigin.Begin); 24 var byteArray = new byte[ms.Length]; 25 ms.Read(byteArray, 0, byteArray.Length); 26 result = Encoding.UTF8.GetString(byteArray); 27 } 28 response.Close(); 29 response.Dispose(); 30 return result; 31 }