在文件 StreamWriter 范围内如何获取文件的总行数

huangapple go评论98阅读模式
英文:

Within a File StreamWriter scope how to get the Total lines counts in a file

问题

无法获得文件的总行数,因为在 StreamWriter 作用域内尝试读取文件会导致文件被另一个进程使用,从而引发错误消息。

英文:

How to do get the Total lines of the file when we are within in a StreamWriter scope.

Based on the total number of lines count I am writing some more lines at the end of the file.

I have tried the below code : But it throws an error message
The process cannot access the file ‘C:\a.txt ' because it is being used by another process.

  1. var lineCount = File.ReadLines(outputFilePath).Count()

This is my Code

  1. private string CreateAndPushFile(string fileName)
  2. {
  3. string outputFilePath = string.Format(@"{0}\{1}", C:\\a.txt”, fileName);
  4. using (StreamWriter output = new StreamWriter(outputFilePath))
  5. {
  6. // Creates the file header
  7. string fileHeader =”kjhakljdhkjhkj”;
  8. output.Write(fileHeader);
  9. string batchControl = 1515151”; // This value comes from database
  10. output.Write(batchControl);
  11. // Here there is some other logic which will writes many lines to the File using foreach loop
  12. string fileControl = 3123123”; // This value comes from database
  13. output.WriteLine(fileControl);
  14. // After this I need write a few more lines only if total number of lines in a File Total records multiple of 10
  15. var lineCount = File.ReadLines(outputFilePath).Count(); // I am getting error here
  16. int remainder;
  17. Math.DivRem(lineCount, 10, out remainder);
  18. for (int i = 1; i <= 10 - remainder; i++)
  19. {
  20. output.WriteLine(“9999999999999”);
  21. }
  22. }
  23. }

答案1

得分: 1

  1. private static void CreateAndPushFile(string outputFilePath) {
  2. using (var output = new StreamWriter(outputFilePath)) {
  3. // 创建文件头部
  4. var fileHeader = "kjhakljdhkjhkj";
  5. output.Write(fileHeader);
  6. var batchControl = "1515151"; // 此值来自数据库
  7. output.Write(batchControl);
  8. // 这里有一些其他逻辑,将使用 foreach 循环写入文件的许多行
  9. var fileControl = "3123123"; // 此值来自数据库
  10. output.WriteLine(fileControl);
  11. // 在此之后,只有在文件中的总行数是 10 的倍数时,才需要写入更多行
  12. }
  13. var lineCount = TotalLines(outputFilePath); // 在这里我遇到了错误
  14. var remainder = lineCount % 10;
  15. using (var output2 = new StreamWriter(outputFilePath, true)) { // 第二个参数用于追加
  16. for (var i = 0; i < 10 - remainder; i++) {
  17. output2.WriteLine("9999999999999");
  18. }
  19. }
  20. }
  21. private static int TotalLines(string filePath) {
  22. using (var reader = new StreamReader(filePath)) {
  23. char[] buffer = new char[1024];
  24. var lineCount = 0;
  25. while (!reader.EndOfStream) {
  26. var charsRead = reader.Read(buffer, 0, 1024);
  27. lineCount += buffer.Take(charsRead).Count(character => character == '\n');
  28. }
  29. return lineCount;
  30. }
  31. }
英文:
  1. private static void CreateAndPushFile(string outputFilePath) {
  2. using (var output = new StreamWriter(outputFilePath)) {
  3. // Creates the file header
  4. var fileHeader = &quot;kjhakljdhkjhkj&quot;;
  5. output.Write(fileHeader);
  6. var batchControl = &quot;1515151&quot;; // This value comes from database
  7. output.Write(batchControl);
  8. // Here there is some other logic which will writes many lines to the File using foreach loop
  9. var fileControl = &quot;3123123&quot;; // This value comes from database
  10. output.WriteLine(fileControl);
  11. // After this I need write a few more lines only if total number of lines in a File Total records multiple of 10
  12. }
  13. var lineCount = TotalLines(outputFilePath); // I am getting error here
  14. var remainder = lineCount % 10;
  15. using (var output2 = new StreamWriter(outputFilePath, true)) { // second parameter is for append
  16. for (var i = 0; i &lt; 10 - remainder; i++) {
  17. output2.WriteLine(&quot;9999999999999&quot;);
  18. }
  19. }
  20. }
  21. private static int TotalLines(string filePath) {
  22. using (var reader = new StreamReader(filePath)) {
  23. char[] buffer = new char[1024];
  24. var lineCount = 0;
  25. while (!reader.EndOfStream) {
  26. var charsRead = reader.Read(buffer, 0, 1024);
  27. lineCount += buffer.Take(charsRead).Count(character =&gt; character == &#39;\n&#39;);
  28. }
  29. return lineCount;
  30. }
  31. }

huangapple
  • 本文由 发表于 2023年2月14日 21:31:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/75448563.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定