代碼 1:
/// <summary> /// 功能:FileStream文件流讀取文件 /// </summary> /// <param name="filePath">參數:文件路徑</param> /// <returns>返回值:StreamReader對象</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; }
代碼 2:
/// <summary> /// 功能:FileStream文件流寫文件 /// </summary> /// <param name="filePath">參數:文件路徑</param> /// <returns>返回值:StreamWriter對象</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; }
調用這兩個方法讀寫文件的調用方式我就不寫了