C# TXT文件的读取和写入

    /// <summary>
        /// 文件的写入
        /// </summary>
        /// <param name="path">文件路径</param>
        /// <param name="name">文件名称</param>
        /// <param name="contents">写入文件的内容</param>
        /// <returns></returns>
        public static KeyValuePair<bool, string> CreateFile(string path, string name, string contents)
        {
            name = path + "\\" + name;
            var pair = new KeyValuePair<bool, string>(true, name);

            //验证路径是否存在
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            try
            {
                FileStream fs;
                //验证文件是否存在,有先清空文本内容,然后追加,否则则创建
                if (File.Exists(name))
                {
                    ClearTxt(name);
                    fs = new FileStream(name, FileMode.Append, FileAccess.Write);
                }
                else
                {
                    fs = new FileStream(name, FileMode.Create, FileAccess.Write);
                }

                //这里注意是默认编码 ANSI,不需要utf-8 
                using (var sw = new StreamWriter(fs, Encoding.Default))
                {
                    sw.Write(contents);
                    sw.Close();
                }
                fs.Close();
            }
            catch (Exception ex)
            {
                pair = new KeyValuePair<bool, string>(false, ex.Message);
            }
            return pair;
        }

        /// <summary>
        /// 清空文本内容
        /// </summary>
        /// <param name="txtPath"></param>
        public static void ClearTxt(string txtPath)
        {
            using (var stream = File.Open(txtPath, FileMode.OpenOrCreate, FileAccess.Write))
            {
                stream.Seek(0, SeekOrigin.Begin);
                stream.SetLength(0);

                stream.Close();
            }
        }
        /// <summary>
        /// 文件的读取
        /// </summary>
        /// <param name="path">文件指定的路径</param>
        /// <returns></returns>
        public static KeyValuePair<bool, List<string>> ReadFile(string path)
        {
            var pair = new KeyValuePair<bool, List<string>>();
            var str = new List<string>();
            try
            {
                using (FileStream fs = new FileStream(path, FileMode.Open))
                {
                    using (StreamReader sr = new StreamReader(fs, Encoding.Default))
                    {
                        while (!sr.EndOfStream)
                        {
                            string sLine = sr.ReadLine();
                            if (sLine.Length < 1)
                            {
                                continue;
                            }
                            str.Add(sLine);
                        }
                    }
                }
                if (str.Contains(""))
                {
                    str.Add("读取文件的内容为空");
                    pair = new KeyValuePair<bool, List<string>>(false, str);
                }
                else
                {
                    pair = new KeyValuePair<bool, List<string>>(true, str);
                }
            }
            catch (Exception ex)
            {
                str.Add(ex.Message);
                pair = new KeyValuePair<bool, List<string>>(false, str);
            }
            return pair;
        }

调用示例:

读取:basCommon.ReadFile("http://*****路径/info.txt")

写入:basCommon.CreateFile("http://*****路径", "文件名称.txt", 文件内容);

发布了92 篇原创文章 · 获赞 12 · 访问量 68万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览