zoukankan      html  css  js  c++  java
  • 读写文本

      1 using System;
      2 using System.Collections.Generic;
      3 using System.Configuration;
      4 using System.IO;
      5 using System.Linq;
      6 using System.Text;
      7 using System.Text.RegularExpressions;
      8 using System.Threading.Tasks;
      9 
     10 namespace MessageText
     11 {
     12     class FileMessage
     13     {
     14         /// <summary>
     15         /// 将信息写入文本中
     16         /// </summary>
     17         /// <param name="AppSettingName"></param>
     18         /// <param name="title"></param>
     19         /// <param name="page"></param>
     20         public static void Write(string path,string title,string text,char i)
     21         {
     22             if (path != null && path != "")
     23             {
     24                 if (!(Directory.Exists(path)))
     25                 {
     26                     Directory.CreateDirectory(path); //创建日志文件夹
     27                 }
     28                 path += string.Format(@"\{0}.txt", title);
     29 
     30                 using (var sw = new StreamWriter(path, true, Encoding.Default))
     31                 {
     32                     sw.Write(text + i);
     33                 }
     34             }
     35         }
     36 
     37         /// <summary>
     38         /// 读取文本信息,若无此文本则创建并返回null
     39         /// </summary>
     40         /// <param name="path"></param>
     41         /// <param name="title"></param>
     42         /// <returns></returns>
     43         public static string Read(string path, string title)
     44         {
     45             string text =null;
     46             if (path != null && path != "")
     47             {
     48                 if (!Directory.Exists(path)) 
     49                 {
     50                     Directory.CreateDirectory(path);
     51                 }
     52                 path += string.Format(@"\{0}.txt", title);
     53 
     54                 using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
     55                 {
     56                     var sr = new StreamReader(stream, Encoding.Default);
     57                     string str = Regex.Replace(sr.ReadToEnd(), @"s+", "");
     58                     if (str == "") return null;
     59                     else text = str;
     60                     sr.Close();
     61                 }
     62             }
     63             return text;
     64         }
     65 
     66         /// <summary>
     67         /// 读取文本信息并分割为数组,若无此文本则创建并返回null
     68         /// </summary>
     69         /// <param name="path"></param>
     70         /// <param name="title"></param>
     71         /// <returns></returns>
     72         public static string[] Read(string path, string title,char i)
     73         {
     74             string[] text = null;
     75             if (path != null && path != "")
     76             {
     77                 if (!Directory.Exists(path))
     78                 {
     79                     Directory.CreateDirectory(path);
     80                 }                    
     81                 path += string.Format(@"\{0}.txt", title);
     82 
     83                 using (var stream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
     84                 {
     85                     var sr = new StreamReader(stream, Encoding.Default);
     86                     string str = Regex.Replace(sr.ReadToEnd(), @"s+", "");
     87                     if (str != "") text = str.Split(i);
     88                     sr.Close();
     89                 }
     90             }
     91             return text;
     92         }
     93 
     94         /// <summary>
     95         /// 读取文本信息并返回,如果没有该文本则返回null,不会创建文本
     96         /// </summary>
     97         /// <param name="AppSettingName"></param>
     98         /// <param name="title"></param>
     99         /// <returns></returns>
    100         public static string ReadText(string path,string title)
    101         {
    102             string text = null;
    103             if (path != null && path != "")
    104             {
    105                 if(!(Directory.Exists(path)))
    106                 {
    107                     Directory.CreateDirectory(path); //创建日志文件夹
    108                 }
    109                 path += string.Format(@"\{0}.txt", title);
    110 
    111                 if (!(File.Exists(path))) return null;
    112 
    113                 using (var sr = new StreamReader(path, Encoding.Default))
    114                 {
    115                     text = Regex.Replace(sr.ReadToEnd(), @"s+", "");
    116                 }
    117             }
    118             return text;
    119         }
    120 
    121         /// <summary>
    122         /// 读取文本信息分割为字符串数组并返回,如果没有该文本则返回null,不会创建文本
    123         /// </summary>
    124         /// <param name="AppSettingName"></param>
    125         /// <param name="title"></param>
    126         /// <returns></returns>
    127         public static string[] ReadText(string path, string title,char i)
    128         {
    129             string[] text = null;
    130             if (path != null && path != "")
    131             {
    132                 if (!(Directory.Exists(path)))
    133                 {
    134                     Directory.CreateDirectory(path); //创建日志文件夹
    135                 }
    136                 path += string.Format(@"\{0}.txt", title);
    137 
    138                 if (!(File.Exists(path))) return null;
    139 
    140                 using (var sr = new StreamReader(path, Encoding.Default))
    141                 {
    142                     string str = Regex.Replace(sr.ReadToEnd(), @"s+", "");
    143                     if (str != "") text = str.Split(i);
    144                 }
    145             }
    146             return text;
    147         }
    148 
    149         /// <summary>
    150         /// 读取末尾页数,若无结果则返回0
    151         /// </summary>
    152         /// <param name="AppSettingName"></param>
    153         /// <param name="title"></param>
    154         /// <param name="i"></param>
    155         /// <returns></returns>
    156         public static int ReadPage(string path, string title, char i)
    157         {
    158             int page = 0;
    159             string[] pageNum = null;
    160             if (path != null && path != "")
    161             {
    162                 if (!(Directory.Exists(path))) Directory.CreateDirectory(path); //创建日志文件夹
    163 
    164                 path += string.Format(@"\{0}.txt", title);
    165 
    166                 using (var fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Read))
    167                 {
    168                     StreamReader sr = new StreamReader(fs, Encoding.Default);
    169                     string str = Regex.Replace(sr.ReadToEnd(), @"s+", "");
    170                     if (str != "") pageNum = str.Split(i);
    171 
    172                     if (pageNum != null)
    173                     {
    174                         page = Convert.ToInt32(pageNum[pageNum.Length - 2]);
    175                     }
    176                     sr.Close();
    177                 }
    178             }
    179             return page;
    180         }
    181 
    182        
    183     }
    184 }
    世界上最可怕事情是比我们优秀的人比我们还努力
  • 相关阅读:
    zbb20180930 Postman 使用方法详解
    Cookie、Session、jsp、EL、JSTL
    Http协议、Tomcat、servlet
    xml、网络编程、 反射
    JDBC、DBUtils
    Java IO流对象、多线程
    mySql
    Java基础
    VueJs
    GIT
  • 原文地址:https://www.cnblogs.com/AlexOneBlogs/p/7287367.html
Copyright © 2011-2022 走看看