首页 > 编程语言 >C# 文件读取FileHelper

C# 文件读取FileHelper

时间:2022-12-27 16:11:44浏览次数:41  
标签:StreamWriter 读取 filePath C# new FileHelper dir string

using System.IO;
using System.Text;

namespace Callno.Common
{
    public class FileHelper
    {
        //定义一个用于保存静态变量的实例
        private static FileHelper instance = null;
        //定义一个保证线程同步的标识
        private static readonly object locker = new object();
        //构造函数为私有,使外界不能创建该类的实例
        private FileHelper() { }
        public static FileHelper Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null) instance = new FileHelper();
                    }
                }
                return instance;
            }
        }

        public void SaveLogToTxt(string dir, string filePath, string text, bool isAppend = false)
        {
            if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);
            filePath = filePath + ".txt";
            using (StreamWriter sw = new StreamWriter(filePath, isAppend))
            {
                sw.WriteLine(text);
                sw.Flush();
                sw.Close();
            }
        }

        public void SaveText(string dir, string fileName, string WriteTxt)
        {
            try
            {
                if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);

                //创建一个文件流,用以写入或者创建一个StreamWriter
                FileStream fs = new FileStream(dir + "\\" + fileName + ".txt", 
                    FileMode.OpenOrCreate, FileAccess.Write);
                StreamWriter streamWriter = new StreamWriter(fs);
                streamWriter.Flush();
                // 使用StreamWriter来往文件中写入内容
                streamWriter.BaseStream.Seek(0, SeekOrigin.End);
                // 把richTextBox1中的内容写入文件
                streamWriter.WriteLine(WriteTxt);
                //关闭此文件
                streamWriter.Flush();
                streamWriter.Close();
            }
            catch { }
        }

        public string ReadAllText(string filePath)
        {
            return File.ReadAllText(filePath);
        }

        public string ReadFileStream(string filePath)
        {
            string result = string.Empty;
            try
            {
                FileStream fileStream = new FileStream(filePath, FileMode.Open, 
                    FileAccess.ReadWrite, FileShare.ReadWrite);
                // 第二步 创建读写器
                StreamReader sr = new StreamReader(fileStream, Encoding.Default);
                //         第三步:读取操作
                //sr.ReadLine()   读取一行     可用  sr.EndOfStream  判断是否到了文件尾
                //   sr.ReadToEnd(); // 读取到最后(读取全部)
                result = sr.ReadToEnd();
                sr.Close(); /*第四步:关闭读取器  */
                fileStream.Close(); /*第五步:关闭文件流*/
            }
            catch { return result; }
            return result;
        }
    }
}

  

标签:StreamWriter,读取,filePath,C#,new,FileHelper,dir,string
From: https://www.cnblogs.com/YYkun/p/17008282.html

相关文章