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

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

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.

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

This is my Code

private string CreateAndPushFile(string fileName)
        {
            string outputFilePath = string.Format(@"{0}\{1}", “C:\\a.txt”, fileName);
            using (StreamWriter output = new StreamWriter(outputFilePath))
             {
                // Creates the file header
                string fileHeader =”kjhakljdhkjhkj”;
                output.Write(fileHeader);
                string batchControl = “1515151”; // This value comes from database
                output.Write(batchControl);

   // Here there is some other logic which will writes many lines to the File using foreach loop

  string fileControl = “3123123”; // This value comes from database 
                output.WriteLine(fileControl);

 // After this I need write a few more lines only if total number of lines in a File Total records multiple of 10

                var lineCount = File.ReadLines(outputFilePath).Count(); // I am getting error here 
                int remainder;
                Math.DivRem(lineCount, 10, out remainder);
                for (int i = 1; i <= 10 - remainder; i++)
                {
                    output.WriteLine(“9999999999999”);
                }
        }
  }

答案1

得分: 1

private static void CreateAndPushFile(string outputFilePath) {
    using (var output = new StreamWriter(outputFilePath)) {
        // 创建文件头部
        var fileHeader = "kjhakljdhkjhkj";
        output.Write(fileHeader);
        var batchControl = "1515151"; // 此值来自数据库
        output.Write(batchControl);

        // 这里有一些其他逻辑,将使用 foreach 循环写入文件的许多行

        var fileControl = "3123123"; // 此值来自数据库
        output.WriteLine(fileControl);

        // 在此之后,只有在文件中的总行数是 10 的倍数时,才需要写入更多行
    }

    var lineCount = TotalLines(outputFilePath); // 在这里我遇到了错误
    var remainder = lineCount % 10;

    using (var output2 = new StreamWriter(outputFilePath, true)) { // 第二个参数用于追加
        for (var i = 0; i < 10 - remainder; i++) {
            output2.WriteLine("9999999999999");
        }
    }
}

private static int TotalLines(string filePath) {
    using (var reader = new StreamReader(filePath)) {
        char[] buffer = new char[1024];
        var lineCount = 0;
        while (!reader.EndOfStream) {
            var charsRead = reader.Read(buffer, 0, 1024);
            lineCount += buffer.Take(charsRead).Count(character => character == '\n');
        }

        return lineCount;
    }
}
英文:
private static void CreateAndPushFile(string outputFilePath) {
    using (var output = new StreamWriter(outputFilePath)) {
        // Creates the file header
        var fileHeader = &quot;kjhakljdhkjhkj&quot;;
        output.Write(fileHeader);
        var batchControl = &quot;1515151&quot;; // This value comes from database
        output.Write(batchControl);

        // Here there is some other logic which will writes many lines to the File using foreach loop

        var fileControl = &quot;3123123&quot;; // This value comes from database 
        output.WriteLine(fileControl);

        // After this I need write a few more lines only if total number of lines in a File Total records multiple of 10
    }

    var lineCount = TotalLines(outputFilePath); // I am getting error here 
    var remainder = lineCount % 10;

    using (var output2 = new StreamWriter(outputFilePath, true)) { // second parameter is for append
        for (var i = 0; i &lt; 10 - remainder; i++) {
            output2.WriteLine(&quot;9999999999999&quot;);
        }
    }
}

private static int TotalLines(string filePath) {
    using (var reader = new StreamReader(filePath)) {
        char[] buffer    = new char[1024];
        var    lineCount = 0;
        while (!reader.EndOfStream) {
            var charsRead = reader.Read(buffer, 0, 1024);
            lineCount += buffer.Take(charsRead).Count(character =&gt; character == &#39;\n&#39;);
        }

        return lineCount;
    }
}

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:

确定