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;
            }
        }
    }
    天生我材必有用,千金散尽还复来
  • 相关阅读:
    关闭。没意思
    2015年8月7日15:18:54工作
    2015年8月4日11:43:00工作内容
    博客功能的转变
    php小知识。
    来杭州的工作一览
    开发一款桌面程序。文件转换器
    解决一个题目。关于结构体与链表的操作
    批量检查APK是否具有指定的权限。
    Skynet Pomelo Erlang Elixir 的认识
  • 原文地址:https://www.cnblogs.com/ligenyun/p/7743461.html
Copyright © 2011-2022 走看看