• ASP.NET 文件操作类


    1.读取文件

    2.写入文件

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    
    namespace BigData.Common.File
    {
        /// <summary>
        /// 文件操作类
        /// </summary>
        public class FileHelper
        {
            /// <summary>
            /// 写入一行数据
            /// </summary>
            /// <param name="path"></param>
            /// <param name="str"></param>
            /// <param name="isAppend"></param>
            /// <returns></returns>
            public static bool WriteLine(string path, string str, bool isAppend = true)
            {
                try
                {
                    StreamWriter sw = new StreamWriter(path, isAppend);
                    sw.WriteLine(str);
                    sw.Flush();
                    sw.Close();
                }
                catch (Exception)
                {
                    return false;
                }
    
                return true;
            }
    
            /// <summary>
            /// 读取文件所有内容
            /// </summary>
            /// <param name="path"></param>
            /// <returns></returns>
            public static string ReadAll(string path)
            {
                string result = string.Empty;
                try
                {
                    StreamReader sr = new StreamReader(path);
                    result = sr.ReadToEnd();
                    sr.Close();
                }
                catch (Exception)
                {
                    return string.Empty;
                }
    
                return result;
            }
    
            /// <summary>
            /// 写入文本
            /// </summary>
            /// <param name="path"></param>
            /// <param name="str"></param>
            /// <returns></returns>
            public static bool WriteToTxt(string path, string str)
            {
                try
                {
                    FileStream fs = new FileStream(path, FileMode.Append);
                    byte[] bs = Encoding.Default.GetBytes(str);
                    fs.Write(bs, 0, bs.Length);
                    fs.Flush();
                    fs.Close();
                }
                catch (Exception)
                {
                    return false;
                }
    
                return true;
            }
    
            /// <summary>
            /// 读取文本
            /// </summary>
            /// <param name="path"></param>
            /// <returns></returns>
            public static string ReadTxt(string path)
            {
                string result = string.Empty;
                try
                {
                    FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
                    byte[] bs = new byte[fs.Length];
                    fs.Read(bs, 0, bs.Length);
                    result = Encoding.Default.GetString(bs);
                    fs.Close();
                }
                catch (Exception)
                {
                    return string.Empty;
                }
    
                return result;
            }
        }
    }
    天生我材必有用,千金散尽还复来
  • 相关阅读:
    Mysql权限控制
    Linux查看端口
    linus 下redis守护进程启动
    pymongo创建索引
    mongo批量操作存在更新否则插入
    梯度下降推导过程资料整理
    [转]mitmproxy套件使用攻略及定制化开发
    终极利器!利用appium和mitmproxy登录获取cookies
    how-to-pass-a-class-variable-to-a-decorator-inside-class-definition
    python进阶之魔法函数
  • 原文地址:https://www.cnblogs.com/ligenyun/p/7743461.html
Copyright © 2020-2023  润新知