TxtTool.cs 3.6 KB

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