/// <summary> /// 多行字符串顺序写入文件 /// </summary> /// <param name="enumerable"></param> /// <param name="fileInfo">文件信息</param> /// <param name="append">是否追加, 默认是不追加</param> /// <param name="encoding">写入编码, 默认是Unicode</param> public static void WriteToFile(this IEnumerable <string> enumerable, FileInfo fileInfo, bool append = false, Encoding?encoding = null) { using var writer = StreamWriterHelper.NewStreamWriterByFilepath(fileInfo.FullName, append, encoding); foreach (var line in enumerable) { writer.WriteLine(line); } }
/// <summary> /// 获取流读取器 /// </summary> /// <param name="stream"></param> /// <param name="encoding">编码. null为系统默认编码</param> /// <returns></returns> public static StreamWriter GetStreamWriter(this Stream stream, Encoding?encoding = null) => StreamWriterHelper.NewStreamWriterToStream(stream, encoding);
/// <summary> /// 使用指定的编码向指定的文件写入字符串,可选追加或者创建/覆盖. /// </summary> /// <param name="filepath">写入文件</param> /// <param name="content">写入的内容</param> /// <param name="append">是否追加. true为追加, false为创建或覆盖</param> /// <param name="encoding">指定的编码方法</param> public static void WriteStringToFile(string filepath, string content, bool append = false, Encoding?encoding = null) { using var writer = StreamWriterHelper.NewStreamWriterByFilepath(filepath, append, encoding); writer.Write(content); writer.Flush(); }