Encapsulation code of IO reading and writing file method of C

  • 2021-07-22 11:05:06
  • OfStack

Specific do not do detailed introduction, directly on the code


/// <summary>
  ///  Functions: 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>
  ///  Functions: FileStream File stream writes 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;
  }

The above code for IO read-write file encapsulation method to do a detailed introduction, hoping to help everyone.


Related articles: