TxtTool.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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)
  49. {
  50. try
  51. {
  52. DirTool.Create(Path.GetDirectoryName(file));
  53. using (StreamWriter sw = new StreamWriter(file, false, Encoding.UTF8))
  54. {
  55. sw.WriteLine(txt);
  56. }
  57. return true;
  58. }
  59. catch (Exception e) { }
  60. return false;
  61. }
  62. public static string Read(string file)
  63. {
  64. try
  65. {
  66. if (File.Exists(file))
  67. {
  68. using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
  69. {
  70. string result = "", line;
  71. while ((line = sr.ReadLine()) != null)
  72. result += line.ToString();
  73. return result;
  74. }
  75. }
  76. }
  77. catch { }
  78. return null;
  79. }
  80. public static string Read(string file, Encoding encoding)
  81. {
  82. try
  83. {
  84. if (File.Exists(file))
  85. {
  86. using (StreamReader sr = new StreamReader(file, encoding))
  87. {
  88. string result = "", line;
  89. while ((line = sr.ReadLine()) != null)
  90. result += line.ToString();
  91. return result;
  92. }
  93. }
  94. }
  95. catch { }
  96. return null;
  97. }
  98. public static List<string> ReadLine(string file)
  99. {
  100. try
  101. {
  102. using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
  103. {
  104. List<string> result = new List<string>();
  105. string line;
  106. while ((line = sr.ReadLine()) != null)
  107. result.Add(line.ToString());
  108. return result;
  109. }
  110. }
  111. catch (Exception e) { }
  112. return null;
  113. }
  114. public static List<string> ReadLine(string file, Encoding encoding)
  115. {
  116. try
  117. {
  118. using (StreamReader sr = new StreamReader(file, encoding))
  119. {
  120. List<string> result = new List<string>();
  121. string line;
  122. while ((line = sr.ReadLine()) != null)
  123. result.Add(line.ToString());
  124. return result;
  125. }
  126. }
  127. catch (Exception e) { }
  128. return null;
  129. }
  130. public static void ReadLine(string file, Action<int, string> action)
  131. {
  132. try
  133. {
  134. using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
  135. {
  136. string line;
  137. int number = 1;
  138. while ((line = sr.ReadLine()) != null)
  139. action.Invoke(number++, line);
  140. }
  141. }
  142. catch (Exception e) { }
  143. }
  144. public static void ReadLine(string file, Encoding encoding, Action<int, string> action)
  145. {
  146. try
  147. {
  148. using (StreamReader sr = new StreamReader(file, encoding))
  149. {
  150. string line;
  151. int number = 1;
  152. while ((line = sr.ReadLine()) != null)
  153. action.Invoke(number++, line);
  154. }
  155. }
  156. catch (Exception e) { }
  157. }
  158. public static long CountLine(string file, string[] filter)
  159. {
  160. long count = 0;
  161. try
  162. {
  163. using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
  164. {
  165. string line;
  166. while ((line = sr.ReadLine()) != null)
  167. {
  168. bool access = true;
  169. if (!ListTool.IsNullOrEmpty(filter))
  170. {
  171. foreach (var f in filter)
  172. {
  173. if (line.Trim() == f) access = false;
  174. }
  175. }
  176. if (access) count++;
  177. }
  178. }
  179. }
  180. catch (Exception e) { }
  181. return count;
  182. }
  183. /// <summary>
  184. /// 替换执行文件文本块
  185. /// </summary>
  186. /// <param name="file"></param>
  187. /// <param name="begString"></param>
  188. /// <param name="endString"></param>
  189. /// <param name="content"></param>
  190. /// <returns></returns>
  191. public static bool ReplaceBlock(string file, string begString, string endString, List<string> content)
  192. {
  193. if (Str.Ok(file) && File.Exists(file))
  194. {
  195. List<string> result = new List<string>();
  196. int begIndex = 0, endIndex = 0;
  197. // 找到标记位置
  198. List<string> txt = TxtTool.ReadLine(file);
  199. if (Ls.Ok(txt))
  200. {
  201. for (int i = 0; i < txt.Count; i++)
  202. {
  203. // 找到要替换内容的开始行和结束行
  204. if (txt[i].StartsWith(begString)) begIndex = i;
  205. if (txt[i].StartsWith(endString)) endIndex = i;
  206. }
  207. }
  208. else
  209. {
  210. txt = new List<string>();
  211. }
  212. // 整理输出内容
  213. if (begIndex < endIndex)
  214. {
  215. List<string> upPart = txt.GetRange(0, begIndex + 1);
  216. List<string> downPart = txt.GetRange(endIndex, txt.Count - endIndex);
  217. result.AddRange(upPart);
  218. result.AddRange(content);
  219. result.AddRange(downPart);
  220. }
  221. else
  222. {
  223. result.AddRange(txt);
  224. result.Add("");
  225. result.Add(begString);
  226. result.AddRange(content);
  227. result.Add(endString);
  228. }
  229. // 写出文件
  230. try
  231. {
  232. var utf8WithoutBom = new UTF8Encoding(false);
  233. using (StreamWriter sw = new StreamWriter(file, false, utf8WithoutBom))
  234. {
  235. foreach (var line in result)
  236. {
  237. sw.WriteLine(line);
  238. }
  239. }
  240. return true;
  241. }
  242. catch (Exception ex) { }
  243. }
  244. return false;
  245. }
  246. }
  247. }