/// <summary> /// テキスト形式のファイルに出力する。 /// </summary> /// <param name="path">ファイルのパス</param> /// <param name="txt">書き込むテキスト</param> /// <param name="option">エンコードのオプション</param> /// <param name="mode">書き込み方のモード</param> /// <exception cref="ArgumentException">filenameが空文字または指定エンコードが不正な値だった場合</exception> /// <exception cref="ArgumentNullException">filenameがnull</exception> /// <exception cref="DirectoryNotFoundException">ファイルのパスが存在しない</exception> /// <exception cref="FileNotFoundException">ファイルが見つからない</exception> /// <exception cref="NotSupportedException">ファイルパスが不正な値だった場合</exception> /// <exception cref="OutOfMemoryException">メモリが不足している</exception> /// <exception cref="IOException"><see cref="System.IO"/>上のエラー</exception> public static void WriteText(string path, string txt, EncodeOption option, WriteTextMode mode) { using var text = new StreamWriter(path, false, option.Encoding); switch (mode) { case WriteTextMode.Overwrite: text.Write(txt); return; case WriteTextMode.AddBeggining: var t1 = ReadTextS(path, option); text.WriteLine(txt); foreach (var t in t1) { text.WriteLine(t); } return; case WriteTextMode.AddEnd: var t2 = ReadTextS(path, option); foreach (var t in t2) { text.WriteLine(t); } text.Write(txt); return; } }
/// <summary> /// Altseedを使ってcsvファイルからテキストを読み込む。 /// 行ごとにコレクションの要素になる。 /// </summary> /// <param name="path">読み込むファイル名</param> /// <param name="option">エンコードのオプション</param> /// <param name="splitWord">文字列を分割する単語</param> /// <exception cref="ArgumentException">filenameが空文字または指定エンコードが不正な値だった場合</exception> /// <exception cref="ArgumentNullException">filenameがnull</exception> /// <exception cref="DecoderFallbackException">フォールバックが発生</exception> public static IEnumerable <string[]> ReadTextA(string path, EncodeOption option, params char[] splitWord) { var file = Engine.File.CreateStaticFile(path); var text = option.Encoding.GetString(file.Buffer); var csv = text.Split('\n').Select(x => x.Split(splitWord)).ToList(); return(csv); }
/// <summary> /// Altseedを使ってcsvファイルからテキストを読み込む。 /// 行ごとにコレクションの要素になる。 /// </summary> /// <param name="path">読み込むファイル名</param> /// <param name="option">エンコードのオプション</param> /// <exception cref="ArgumentException">filenameが空文字または指定エンコードが不正な値だった場合</exception> /// <exception cref="ArgumentNullException">filenameがnull</exception> /// <exception cref="DecoderFallbackException">フォールバックが発生</exception> public static IEnumerable <string> ReadTextA(string path, EncodeOption option) { var file = Engine.File.CreateStaticFile(path); var text = option.Encoding.GetString(file.Buffer); var csv = text.Split('\n').ToList(); return(csv); }
/// <summary> /// <see cref="StreamReader"/>を使ってcsvファイルからテキストを読み込む。 /// 行ごとにコレクションの要素になる。 /// </summary> /// <param name="path">読み込むファイル名</param> /// <param name="option">エンコードのオプション</param> /// <exception cref="ArgumentException">filenameが空文字または指定エンコードが不正な値だった場合</exception> /// <exception cref="ArgumentNullException">filenameがnull</exception> /// <exception cref="DirectoryNotFoundException">ファイルのパスが存在しない</exception> /// <exception cref="FileNotFoundException">ファイルが見つからない</exception> /// <exception cref="NotSupportedException">ファイルパスが不正な値だった場合</exception> /// <exception cref="OutOfMemoryException">メモリが不足している</exception> /// <exception cref="IOException"><see cref="System.IO"/>上のエラー</exception> public static IEnumerable <string> ReadTextS(string path, EncodeOption option) { var list = new List <string>(); using (var streamReader = new StreamReader(path, option.Encoding)) while (!streamReader.EndOfStream) { string line = streamReader.ReadLine(); list.Add(line); } return(list); }
/// <summary> /// <see cref="StreamReader"/>を使ってcsvファイルからテキストを読み込む。 /// 行ごとに分割された文字列がコレクションの要素になる。 /// </summary> /// <param name="path">読み込むファイル名</param> /// <param name="splitWord">文字列を分割する単語</param> /// <param name="option">エンコードのオプション</param> /// <exception cref="ArgumentException">filenameが空文字または指定エンコードが不正な値だった場合</exception> /// <exception cref="ArgumentNullException">filenameがnull</exception> /// <exception cref="DirectoryNotFoundException">ファイルのパスが存在しない</exception> /// <exception cref="FileNotFoundException">ファイルが見つからない</exception> /// <exception cref="NotSupportedException">ファイルパスが不正な値だった場合</exception> /// <exception cref="OutOfMemoryException">メモリが不足している</exception> /// <exception cref="IOException"><see cref="System.IO"/>上のエラー</exception> public static IEnumerable <string[]> ReadTextS(string path, EncodeOption option, params char[] splitWord) { var list = new List <string[]>(); using (var streamReader = new StreamReader(path, option.Encoding)) while (!streamReader.EndOfStream) { string line = streamReader.ReadLine(); var words = line.Split(splitWord); list.Add(words); } return(list); }