zoukankan      html  css  js  c++  java
  • 【C#】 TxtHelper

    public static class TxtHelper
    {
    /// <summary>
    /// 在txt文件中加一条数据
    /// </summary>
    /// <param name="folderName">文件夹名称</param>
    /// <param name="fileName">文件名称</param>
    /// <param name="line">信息</param>
    public static void WriteLine(string folderName, string fileName, string line)
    {
    string LogPath = folderName;
    if (!Directory.Exists(LogPath))
    {
    Directory.CreateDirectory(LogPath);
    }
    string filePath = LogPath + fileName;
    using (FileStream stream = File.Open(filePath, FileMode.Append, FileAccess.Write, FileShare.Write))
    {
    using (StreamWriter sw = new StreamWriter(stream))
    {
    sw.WriteLine(line);
    }
    }
    }
    /// <summary>
    /// 在txt文件中加多条数据
    /// </summary>
    /// <param name="folderName">文件夹名称</param>
    /// <param name="fileName">文件名称</param>
    /// <param name="lines">多条信息</param>
    public static void WriteLines(string folderName, string fileName, List<string> lines)
    {
    string LogPath = folderName;
    if (!Directory.Exists(LogPath))
    {
    Directory.CreateDirectory(LogPath);
    }
    string filePath = LogPath + fileName;
    using (FileStream stream = File.Open(filePath, FileMode.Append, FileAccess.Write, FileShare.Write))
    {
    using (StreamWriter sw = new StreamWriter(stream))
    {
    lines.ForEach(e =>
    {
    sw.WriteLine(e);

    });
    }
    }
    }
    /// <summary>
    /// 在txt文件中覆盖一条数据
    /// </summary>
    /// <param name="folderName">文件夹名称</param>
    /// <param name="fileName">文件名称</param>
    /// <param name="line">信息</param>
    public static void ReWriteLine(string folderName, string fileName, string line)
    {
    string LogPath = folderName;
    if (!Directory.Exists(LogPath))
    {
    Directory.CreateDirectory(LogPath);
    }
    string filePath = LogPath + fileName;
    using (FileStream stream = File.Open(filePath, FileMode.Create, FileAccess.Write, FileShare.Write))
    {
    using (StreamWriter sw = new StreamWriter(stream))
    {
    sw.WriteLine(line);
    }
    }
    }
    /// <summary>
    /// 在txt文件中覆盖多条数据
    /// </summary>
    /// <param name="folderName">文件夹名称</param>
    /// <param name="fileName">文件名称</param>
    /// <param name="line">信息</param>
    public static void ReWriteLines(string folderName, string fileName, List<string> lines)
    {
    string LogPath = folderName;
    if (!Directory.Exists(LogPath))
    {
    Directory.CreateDirectory(LogPath);
    }
    string filePath = LogPath + fileName;
    using (FileStream stream = File.Open(filePath, FileMode.Create, FileAccess.Write, FileShare.Write))
    {
    using (StreamWriter sw = new StreamWriter(stream))
    {
    lines.ForEach(e =>
    {
    sw.WriteLine(e);

    });
    }
    }
    }
    /// <summary>
    /// 在txt文件中取出所有数据
    /// </summary>
    /// <param name="folderName">文件夹名称</param>
    /// <param name="fileName">文件名称</param>
    public static List<string> GetAllLines(string folderName, string fileName)
    {
    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
    string LogPath = folderName;
    if (!Directory.Exists(LogPath))
    {
    Directory.CreateDirectory(LogPath);
    File.WriteAllText(folderName + fileName, "");
    }
    string filePath = LogPath + fileName;

    FileStream stream = null;
    StreamReader reader = null;
    List<string> baseList = new List<string>();
    try
    {
    stream = new FileStream(filePath, FileMode.Open);
    reader = new StreamReader(stream, Encoding.GetEncoding("utf-8"));

    bool isNotNullLine = true;

    while (isNotNullLine)
    {
    string line = reader.ReadLine();
    if (string.IsNullOrEmpty(line))
    {
    isNotNullLine = false;
    }
    else
    {
    baseList.Add(line);
    }
    }
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    }
    finally
    {
    if (stream != null)
    {
    stream.Close();
    stream.Dispose();
    }
    if (reader != null)
    {
    reader.Close();
    reader.Dispose();
    }
    }
    if (baseList.Count == 0)
    {
    return new List<string>();
    }
    return baseList;

    }
    /// <summary>
    /// 在txt文件中取出输入行之后的所有数据
    /// </summary>
    /// <param name="folderName">文件夹名称</param>
    /// <param name="fileName">文件名称</param>
    public static List<string> GetNextAllLines(string folderName, string fileName, string inputline)
    {
    Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);
    string LogPath = folderName;
    if (!Directory.Exists(LogPath))
    {
    Directory.CreateDirectory(LogPath);
    }
    string filePath = LogPath + fileName;

    FileStream stream = null;
    StreamReader reader = null;
    List<string> baseList = new List<string>();
    try
    {
    stream = new FileStream(filePath, FileMode.Open);
    reader = new StreamReader(stream, Encoding.GetEncoding("utf-8"));

    bool isNotNullLine = true;

    while (isNotNullLine)
    {
    string line = reader.ReadLine();
    if (string.IsNullOrEmpty(line))
    {
    isNotNullLine = false;
    }
    else
    {
    if (line == inputline)
    {
    break;
    }

    }
    }

    while (isNotNullLine)
    {
    string line = reader.ReadLine();
    if (string.IsNullOrEmpty(line))
    {
    isNotNullLine = false;
    }
    else
    {

    baseList.Add(line);
    }
    }
    }
    catch (Exception ex)
    {
    Console.WriteLine(ex.Message);
    }
    finally
    {
    if (stream != null)
    {
    stream.Close();
    stream.Dispose();
    }
    if (reader != null)
    {
    reader.Close();
    reader.Dispose();
    }
    }
    return baseList;

    }
    /// <summary>
    /// 获取文件夹下所有后缀是fileType文件
    /// </summary>
    /// <param name="folderName">文件夹</param>
    /// <param name="fileType">文件类型</param>
    /// <returns></returns>
    public static List<string> GetAllFiles(string folderName, string fileType)
    {
    var files = Directory.GetFiles(folderName, $"*.{fileType}");
    List<string> historyList = new List<string>();
    foreach (var file in files)
    historyList.Add(Path.GetFileName(file));
    return historyList;
    }

    }

  • 相关阅读:
    BP神经网络基本原理
    天将降大任于斯人也,必先苦其心志,劳其筋骨,饿其体肤,空乏其身,行拂乱其所为,所以动心忍性,增益其所不能
    LSH算法原理
    数据库索引的作用和长处缺点
    开机黑屏 仅仅显示鼠标 电脑黑屏 仅仅有鼠标 移动 [已成功解决]
    Linux makefile 教程 很具体,且易懂
    银行家算法
    HDU 1757 A Simple Math Problem(矩阵高速幂)
    js中substr与substring的差别
    BackTrack5 (BT5)无线password破解教程之WPA/WPA2-PSK型无线password破解
  • 原文地址:https://www.cnblogs.com/xuxml/p/12192037.html
Copyright © 2011-2022 走看看