private uint getFileAddress(FileMode?nullableMode = null) { FileMode mode = nullableMode ?? CurrentFileMode; switch (mode) { case FileMode.FileA: return(FileConfig.FileAAddress); case FileMode.FileB: return(FileConfig.FileBAddress); case FileMode.FileC: return(FileConfig.FileCAddress); case FileMode.FileD: return(FileConfig.FileDAddress); case FileMode.FileASaved: return(FileConfig.FileASavedAddress); case FileMode.FileBSaved: return(FileConfig.FileBSavedAddress); case FileMode.FileCSaved: return(FileConfig.FileCSavedAddress); case FileMode.FileDSaved: return(FileConfig.FileDSavedAddress); default: throw new ArgumentOutOfRangeException(); } }
/// <summary> /// 输出字符串到文件 /// 注:使用自定义编码以及写入模式 /// </summary> /// <param name="content">内容</param> /// <param name="path">文件路径</param> /// <param name="encoding">字符编码</param> /// <param name="fileModel">写入模式</param> private static void WriteTxt(string content, string path, Encoding encoding, FileMode?fileModel) { encoding = encoding ?? Encoding.UTF8; fileModel = fileModel ?? FileMode.Create; string dir = Path.GetDirectoryName(path); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } using (FileStream fileStream = new FileStream(path, fileModel.Value)) { using (StreamWriter streamWriter = new StreamWriter(fileStream, encoding)) { streamWriter.Write(content); streamWriter.Flush(); } } }
/// <summary> /// 输出字符串到文件 /// 注:使用自定义编码以及写入模式 /// </summary> /// <param name="content">内容</param> /// <param name="path">文件路径</param> /// <param name="encoding">字符编码</param> /// <param name="fileModel">写入模式</param> private static void WriteTxt(string content, string path, Encoding encoding, FileMode?fileModel) { CheckDirectory(path); if (encoding == null) { encoding = Encoding.Default; } if (fileModel == null) { fileModel = FileMode.Create; } using (FileStream fileStream = new FileStream(path, fileModel.Value)) { using (StreamWriter streamWriter = new StreamWriter(fileStream, encoding)) { streamWriter.Write(content); streamWriter.Flush(); } } }
/// <summary> /// Creates a Sink which writes incoming <see cref="ByteString"/> elements to the given file and either overwrites /// or appends to it. /// /// Materializes a <see cref="Task{TResult}"/> of <see cref="IOResult"/> that will be completed with the size of the file(in bytes) at the streams completion, /// and a possible exception if IO operation was not completed successfully. /// /// This source is backed by an Actor which will use the dedicated "akka.stream.blocking-io-dispatcher", /// unless configured otherwise by using <see cref="ActorAttributes"/>. /// </summary> /// <param name="f">the file to write to</param> /// <param name="fileMode">the write file mode, defaults to <see cref="FileMode.OpenOrCreate"/></param> /// <param name="startPosition">the start position to write to, defaults to 0</param> /// <returns>TBD</returns> public static Sink <ByteString, Task <IOResult> > ToFile(FileInfo f, FileMode?fileMode = null, long startPosition = 0) => new Sink <ByteString, Task <IOResult> >(new FileSink(f, startPosition, fileMode ?? FileMode.OpenOrCreate, DefaultAttributes.FileSink, new SinkShape <ByteString>(new Inlet <ByteString>("FileSink"))));
/// <summary> /// Creates a Sink which writes incoming <see cref="ByteString"/> elements to the given file. Overwrites existing files /// by truncating their contents as default. /// /// Materializes a <see cref="Task{TResult}"/> of <see cref="IOResult"/> that will be completed with the size of the file(in bytes) at the streams completion, /// and a possible exception if IO operation was not completed successfully. /// /// This source is backed by an Actor which will use the dedicated "akka.stream.blocking-io-dispatcher", /// unless configured otherwise by using <see cref="ActorAttributes"/>. /// </summary> /// <param name="f">the file to write to</param> /// <param name="fileMode">the write file mode, defaults to <see cref="FileMode.OpenOrCreate"/></param> /// <param name="startPosition">the start position to write to, defaults to 0</param> /// <param name="autoFlush">when set, auto flush the file buffer to disk for every incoming element</param> /// <param name="flushSignaler">when passed an instance of <see cref="FlushSignaler"/>, can be used to send a manual flush signal to the file sink</param> /// <returns>TBD</returns> public static Sink <ByteString, Task <IOResult> > ToFile(FileInfo f, FileMode?fileMode = null, long startPosition = 0, bool autoFlush = false, FlushSignaler flushSignaler = null) => new Sink <ByteString, Task <IOResult> >(new FileSink(f, startPosition, fileMode ?? FileMode.Create, DefaultAttributes.FileSink, new SinkShape <ByteString>(new Inlet <ByteString>("FileSink")), autoFlush, flushSignaler));
public static async Task <FileStream> CreateFileAsync(this Random rand, long cbyte = -1, string path = null, FileMode?fileMode = null, FileShare?fileShare = null, FileOptions?fileOptions = null, int?bufferSize = null) { cbyte = cbyte >= 0 ? cbyte : rand.Next(1024 * 1024); path = path ?? Path.GetTempFileName(); fileMode = fileMode ?? FileMode.Create; fileShare = fileShare ?? FileShare.None; bufferSize = bufferSize ?? 4096; fileOptions = fileOptions ?? FileOptions.Asynchronous | FileOptions.DeleteOnClose; var fStream = new FileStream(path, fileMode.Value, FileAccess.ReadWrite, fileShare.Value, bufferSize.Value, fileOptions.Value); var buff = new byte[1024]; for (long rbyte = cbyte; rbyte > 0; rbyte -= 1024) { rand.NextBytes(buff); await fStream.WriteAsync(buff, 0, Convert.ToInt32(Math.Min(rbyte, buff.LongCount()))); } await fStream.FlushAsync(); fStream.Position = 0; return(fStream); }