TxtTool.cs 9.0 KB

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