TxtTool.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 long CountLine(string file, string[] filter)
  96. {
  97. long count = 0;
  98. try
  99. {
  100. using (StreamReader sr = new StreamReader(file, Encoding.UTF8))
  101. {
  102. string line;
  103. while ((line = sr.ReadLine()) != null)
  104. {
  105. bool access = true;
  106. if (!ListTool.IsNullOrEmpty(filter))
  107. {
  108. foreach (var f in filter)
  109. {
  110. if (line.Trim() == f) access = false;
  111. }
  112. }
  113. if (access) count++;
  114. }
  115. }
  116. }
  117. catch (Exception e) { }
  118. return count;
  119. }
  120. }
  121. }