TxtTool.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text;
  5. using Y.Utils.Net20.FileUtils;
  6. using Y.Utils.Net20.ListUtils;
  7. namespace Y.Utils.Net20.TxtUtils
  8. {
  9. public class TxtTool
  10. {
  11. public static bool Append(string file, List<string> txt)
  12. {
  13. try
  14. {
  15. DirTool.Create(Path.GetDirectoryName(file));
  16. using (StreamWriter sw = new StreamWriter(file, true))
  17. {
  18. if (!ListTool.IsNullOrEmpty(txt))
  19. foreach (var t in txt)
  20. sw.WriteLine(t);
  21. }
  22. return true;
  23. }
  24. catch (Exception e) { }
  25. return false;
  26. }
  27. public static bool Append(string file, string txt)
  28. {
  29. try
  30. {
  31. DirTool.Create(Path.GetDirectoryName(file));
  32. using (StreamWriter sw = new StreamWriter(file, true))
  33. {
  34. sw.WriteLine(txt);
  35. }
  36. return true;
  37. }
  38. catch (Exception e) { }
  39. return false;
  40. }
  41. public static bool Create(string file, string txt)
  42. {
  43. try
  44. {
  45. DirTool.Create(Path.GetDirectoryName(file));
  46. using (StreamWriter sw = new StreamWriter(file, false, Encoding.UTF8))
  47. {
  48. sw.WriteLine(txt);
  49. }
  50. return true;
  51. }
  52. catch (Exception e) { }
  53. return false;
  54. }
  55. public static string Read(string file)
  56. {
  57. try
  58. {
  59. using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
  60. {
  61. string result = "", line;
  62. while ((line = sr.ReadLine()) != null)
  63. result += line.ToString() + Environment.NewLine;
  64. return result;
  65. }
  66. }
  67. catch (Exception e) { }
  68. return null;
  69. }
  70. public static string Read(string file, Encoding encoding)
  71. {
  72. try
  73. {
  74. using (StreamReader sr = new StreamReader(file, encoding))
  75. {
  76. string result = "", line;
  77. while ((line = sr.ReadLine()) != null)
  78. result += line.ToString() + Environment.NewLine;
  79. return result;
  80. }
  81. }
  82. catch (Exception e) { }
  83. return null;
  84. }
  85. public static List<string> ReadLine(string file)
  86. {
  87. try
  88. {
  89. using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
  90. {
  91. List<string> result = new List<string>();
  92. string line;
  93. while ((line = sr.ReadLine()) != null)
  94. result.Add(line.ToString());
  95. return result;
  96. }
  97. }
  98. catch (Exception e) { }
  99. return null;
  100. }
  101. public static long CountLine(string file, string[] filter)
  102. {
  103. long count = 0;
  104. try
  105. {
  106. using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
  107. {
  108. string line;
  109. while ((line = sr.ReadLine()) != null)
  110. {
  111. bool access = true;
  112. if (ListTool.HasElements(filter))
  113. {
  114. foreach (var f in filter)
  115. {
  116. if (line.Trim() == f) access = false;
  117. }
  118. }
  119. if (access) count++;
  120. }
  121. }
  122. }
  123. catch (Exception e) { }
  124. return count;
  125. }
  126. }
  127. }