Ejemplo n.º 1
0
        /// <summary>
        /// テキストファイルを読み込む。
        /// 改行コードはCRLF、CR、LFのどれでも可。
        /// </summary>
        /// <param name="path"></param>
        /// <param name="encode"></param>
        /// <returns>読み込んだ文字列</returns>
        public static string ReadAllText(string path, Encoding encode, string afterNewLine)
        {
            var sb = new StringBuilder();

            using (var reader = new TextLinesReader(new StreamReader(path, encode)))
                using (var writer = new StringWriter(sb))
                {
                    reader.ReadAllToWriter(writer, afterNewLine);
                }
            return(sb.ToString());
        }
Ejemplo n.º 2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="path"></param>
        /// <param name="text"></param>
        /// <param name="encode"></param>
        public static void WriteAllText(string path, string text, Encoding encode, string newLine)
        {
            var fileInfo = new FileInfo(path);

            fileInfo.Directory.Create();

            using (var reader = new TextLinesReader(new StringReader(text)))
                using (var writer = new StreamWriter(fileInfo.FullName, false, encode))
                {
                    reader.ReadAllToWriter(writer, newLine);
                }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// テキストファイルをコピーする。エンコード変換、改行コード変換。タイムスタンプは同じにしない。
        /// </summary>
        /// <param name="srcPath"></param>
        /// <param name="srcEncode"></param>
        /// <param name="destPath"></param>
        /// <param name="destEncode"></param>
        /// <param name="newLine"></param>
        public static void CopyTextFileWithEncodeEol(string srcPath, Encoding srcEncode, string destPath, Encoding destEncode, string newLine)
        {
            if (PathUtil.EqualsPath(srcPath, destPath))
            {
                throw new IOException();
            }

            using (var reader = new TextLinesReader(new StreamReader(srcPath, srcEncode)))
                using (var writer = new StreamWriter(destPath, false, destEncode))
                {
                    reader.ReadAllToWriter(writer, newLine);
                }
        }