zoukankan      html  css  js  c++  java
  • 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;
            }
        }
    }
    天生我材必有用,千金散尽还复来
  • 相关阅读:
    redis 实例
    redis 常用命令
    redis sets类型及操作
    简单说说PHP优化那些事
    C# IEnumerable与IQueryable ,IEnumerable与IList ,LINQ理解Var和IEnumerable
    全文搜索引擎 elasticsearch.net
    .net 异步
    并行开发 8.用VS性能向导解剖你的程序
    并行开发 7.简要分析任务与线程池
    并行开发 6.异步编程模型
  • 原文地址:https://www.cnblogs.com/ligenyun/p/7743461.html
Copyright © 2011-2022 走看看