C # encapsulation of IO read/write files,
Code 1:
/// <Summary> /// function: FileStream file stream reads files /// </summary> /// <param name = "filePath"> parameter: file Path </param> /// <returns> return value: StreamReader object </returns> public static StreamReader ReadFileByFs (string filePath) {FileStream Fs = null; StreamReader Sr = null; try {Fs = new FileStream (filePath, FileMode. openOrCreate, FileAccess. read); Sr = new StreamReader (Fs, Encoding. default);} catch (IOException e) {throw e;} return Sr ;}
Code 2:
/// <Summary> /// function: FileStream file stream Write File /// </summary> /// <param name = "filePath"> parameter: file Path </param> /// <returns> return value: StreamWriter object </returns> public static StreamWriter WriteFileByFs (string filePath) {FileStream Fs = null; StreamWriter Sw = null; try {Fs = new FileStream (filePath, FileMode. openOrCreate, FileAccess. write); Sw = new StreamWriter (Fs, Encoding. default);} catch (IOException e) {throw e;} return Sw ;}
I will not write the method for calling these two methods to read and write files.