TxtTool.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. //************************************************************************
  2. // author: yuzhengyang
  3. // date: 2018.3.27 - 2018.6.3
  4. // desc: 工具描述
  5. // Copyright (c) yuzhengyang. All rights reserved.
  6. //************************************************************************
  7. using Azylee.Core.DataUtils.CollectionUtils;
  8. using Azylee.Core.DataUtils.StringUtils;
  9. using Azylee.Core.IOUtils.DirUtils;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.IO;
  13. using System.Text;
  14. namespace Azylee.Core.IOUtils.TxtUtils
  15. {
  16. public class TxtTool
  17. {
  18. public static bool Append(string file, List<string> txt, bool append = true)
  19. {
  20. try
  21. {
  22. DirTool.Create(Path.GetDirectoryName(file));
  23. using (StreamWriter sw = new StreamWriter(file, append))
  24. {
  25. if (!ListTool.IsNullOrEmpty(txt))
  26. foreach (var t in txt)
  27. sw.WriteLine(t);
  28. }
  29. return true;
  30. }
  31. catch (Exception e) { }
  32. return false;
  33. }
  34. public static bool Append(string file, string txt, bool append = true)
  35. {
  36. try
  37. {
  38. DirTool.Create(Path.GetDirectoryName(file));
  39. using (StreamWriter sw = new StreamWriter(file, append))
  40. {
  41. sw.WriteLine(txt);
  42. }
  43. return true;
  44. }
  45. catch (Exception e) { }
  46. return false;
  47. }
  48. public static bool Create(string file, string txt, string encoding="utf-8")
  49. {
  50. try
  51. {
  52. Encoding enc = Encoding.GetEncoding(encoding);
  53. DirTool.Create(Path.GetDirectoryName(file));
  54. using (StreamWriter sw = new StreamWriter(file, false, enc))
  55. {
  56. sw.WriteLine(txt);
  57. }
  58. return true;
  59. }
  60. catch (Exception e) { }
  61. return false;
  62. }
  63. public static string Read(string file)
  64. {
  65. try
  66. {
  67. if (File.Exists(file))
  68. {
  69. using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
  70. {
  71. string result = "", line;
  72. while ((line = sr.ReadLine()) != null)
  73. result += line.ToString();
  74. return result;
  75. }
  76. }
  77. }
  78. catch { }
  79. return null;
  80. }
  81. public static string Read(string file, Encoding encoding)
  82. {
  83. try
  84. {
  85. if (File.Exists(file))
  86. {
  87. using (StreamReader sr = new StreamReader(file, encoding))
  88. {
  89. string result = "", line;
  90. while ((line = sr.ReadLine()) != null)
  91. result += line.ToString();
  92. return result;
  93. }
  94. }
  95. }
  96. catch { }
  97. return null;
  98. }
  99. public static List<string> ReadLine(string file)
  100. {
  101. try
  102. {
  103. using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
  104. {
  105. List<string> result = new List<string>();
  106. string line;
  107. while ((line = sr.ReadLine()) != null)
  108. result.Add(line.ToString());
  109. return result;
  110. }
  111. }
  112. catch (Exception e) { }
  113. return null;
  114. }
  115. public static List<string> ReadLine(string file, Encoding encoding)
  116. {
  117. try
  118. {
  119. using (StreamReader sr = new StreamReader(file, encoding))
  120. {
  121. List<string> result = new List<string>();
  122. string line;
  123. while ((line = sr.ReadLine()) != null)
  124. result.Add(line.ToString());
  125. return result;
  126. }
  127. }
  128. catch (Exception e) { }
  129. return null;
  130. }
  131. public static void ReadLine(string file, Action<int, string> action)
  132. {
  133. try
  134. {
  135. using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
  136. {
  137. string line;
  138. int number = 1;
  139. while ((line = sr.ReadLine()) != null)
  140. action.Invoke(number++, line);
  141. }
  142. }
  143. catch (Exception e) { }
  144. }
  145. public static void ReadLine(string file, Encoding encoding, Action<int, string> action)
  146. {
  147. try
  148. {
  149. using (StreamReader sr = new StreamReader(file, encoding))
  150. {
  151. string line;
  152. int number = 1;
  153. while ((line = sr.ReadLine()) != null)
  154. action.Invoke(number++, line);
  155. }
  156. }
  157. catch (Exception e) { }
  158. }
  159. public static long CountLine(string file, string[] filter)
  160. {
  161. long count = 0;
  162. try
  163. {
  164. using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
  165. {
  166. string line;
  167. while ((line = sr.ReadLine()) != null)
  168. {
  169. bool access = true;
  170. if (!ListTool.IsNullOrEmpty(filter))
  171. {
  172. foreach (var f in filter)
  173. {
  174. if (line.Trim() == f) access = false;
  175. }
  176. }
  177. if (access) count++;
  178. }
  179. }
  180. }
  181. catch (Exception e) { }
  182. return count;
  183. }
  184. /// <summary>
  185. /// 替换执行文件文本块
  186. /// </summary>
  187. /// <param name="file"></param>
  188. /// <param name="begString"></param>
  189. /// <param name="endString"></param>
  190. /// <param name="content"></param>
  191. /// <returns></returns>
  192. public static bool ReplaceBlock(string file, string begString, string endString, List<string> content)
  193. {
  194. if (Str.Ok(file) && File.Exists(file))
  195. {
  196. List<string> result = new List<string>();
  197. int begIndex = 0, endIndex = 0;
  198. // 找到标记位置
  199. List<string> txt = TxtTool.ReadLine(file);
  200. if (Ls.Ok(txt))
  201. {
  202. for (int i = 0; i < txt.Count; i++)
  203. {
  204. // 找到要替换内容的开始行和结束行
  205. if (txt[i].StartsWith(begString)) begIndex = i;
  206. if (txt[i].StartsWith(endString)) endIndex = i;
  207. }
  208. }
  209. else
  210. {
  211. txt = new List<string>();
  212. }
  213. // 整理输出内容
  214. if (begIndex < endIndex)
  215. {
  216. List<string> upPart = txt.GetRange(0, begIndex + 1);
  217. List<string> downPart = txt.GetRange(endIndex, txt.Count - endIndex);
  218. result.AddRange(upPart);
  219. result.AddRange(content);
  220. result.AddRange(downPart);
  221. }
  222. else
  223. {
  224. result.AddRange(txt);
  225. result.Add("");
  226. result.Add(begString);
  227. result.AddRange(content);
  228. result.Add(endString);
  229. }
  230. // 写出文件
  231. try
  232. {
  233. var utf8WithoutBom = new UTF8Encoding(false);
  234. using (StreamWriter sw = new StreamWriter(file, false, utf8WithoutBom))
  235. {
  236. foreach (var line in result)
  237. {
  238. sw.WriteLine(line);
  239. }
  240. }
  241. return true;
  242. }
  243. catch (Exception ex) { }
  244. }
  245. return false;
  246. }
  247. }
  248. }