TxtTool.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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.IOUtils.DirUtils;
  9. using System;
  10. using System.Collections.Generic;
  11. using System.IO;
  12. using System.Text;
  13. namespace Azylee.Core.IOUtils.TxtUtils
  14. {
  15. public class TxtTool
  16. {
  17. public static bool Append(string file, List<string> txt)
  18. {
  19. try
  20. {
  21. DirTool.Create(Path.GetDirectoryName(file));
  22. using (StreamWriter sw = new StreamWriter(file, true))
  23. {
  24. if (!ListTool.IsNullOrEmpty(txt))
  25. foreach (var t in txt)
  26. sw.WriteLine(t);
  27. }
  28. return true;
  29. }
  30. catch (Exception e) { }
  31. return false;
  32. }
  33. public static bool Append(string file, string txt)
  34. {
  35. try
  36. {
  37. DirTool.Create(Path.GetDirectoryName(file));
  38. using (StreamWriter sw = new StreamWriter(file, true))
  39. {
  40. sw.WriteLine(txt);
  41. }
  42. return true;
  43. }
  44. catch (Exception e) { }
  45. return false;
  46. }
  47. public static bool Create(string file, string txt)
  48. {
  49. try
  50. {
  51. DirTool.Create(Path.GetDirectoryName(file));
  52. using (StreamWriter sw = new StreamWriter(file, false, Encoding.UTF8))
  53. {
  54. sw.WriteLine(txt);
  55. }
  56. return true;
  57. }
  58. catch (Exception e) { }
  59. return false;
  60. }
  61. public static string Read(string file)
  62. {
  63. try
  64. {
  65. if (File.Exists(file))
  66. {
  67. using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
  68. {
  69. string result = "", line;
  70. while ((line = sr.ReadLine()) != null)
  71. result += line.ToString();
  72. return result;
  73. }
  74. }
  75. }
  76. catch { }
  77. return null;
  78. }
  79. public static List<string> ReadLine(string file)
  80. {
  81. try
  82. {
  83. using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
  84. {
  85. List<string> result = new List<string>();
  86. string line;
  87. while ((line = sr.ReadLine()) != null)
  88. result.Add(line.ToString());
  89. return result;
  90. }
  91. }
  92. catch (Exception e) { }
  93. return null;
  94. }
  95. public static void ReadLine(string file, Action<int, string> action)
  96. {
  97. try
  98. {
  99. using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
  100. {
  101. string line;
  102. int number = 1;
  103. while ((line = sr.ReadLine()) != null)
  104. action.Invoke(number++, line);
  105. }
  106. }
  107. catch (Exception e) { }
  108. }
  109. public static void ReadLine(string file,Encoding encoding, Action<int, string> action)
  110. {
  111. try
  112. {
  113. using (StreamReader sr = new StreamReader(file, encoding))
  114. {
  115. string line;
  116. int number = 1;
  117. while ((line = sr.ReadLine()) != null)
  118. action.Invoke(number++, line);
  119. }
  120. }
  121. catch (Exception e) { }
  122. }
  123. public static long CountLine(string file, string[] filter)
  124. {
  125. long count = 0;
  126. try
  127. {
  128. using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
  129. {
  130. string line;
  131. while ((line = sr.ReadLine()) != null)
  132. {
  133. bool access = true;
  134. if (!ListTool.IsNullOrEmpty(filter))
  135. {
  136. foreach (var f in filter)
  137. {
  138. if (line.Trim() == f) access = false;
  139. }
  140. }
  141. if (access) count++;
  142. }
  143. }
  144. }
  145. catch (Exception e) { }
  146. return count;
  147. }
  148. }
  149. }