C# .NET StreamWriter:使用 StreamWriter 写入文件时如何跳行?

发布于 2024-11-30 07:19:29 字数 81 浏览 0 评论 0原文

我使用 StreamReader 读取文本文件。 我想写出这个相同的文本文件,除了它的前 4 行和最后 6 行。

我该怎么做?谢谢。

I read in a text file using StreamReader.
I want to write out this same text file EXCEPT its first 4 lines and its last 6 lines.

How do I do this? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

渡你暖光 2024-12-07 07:19:29
string[] fileLines = File.ReadAllLines(@"your file path"); 

var result = fileLines.Skip(4).Take(fileLines.Length - (4 + 6));

File.WriteAllLines(@"your output file path", result);
string[] fileLines = File.ReadAllLines(@"your file path"); 

var result = fileLines.Skip(4).Take(fileLines.Length - (4 + 6));

File.WriteAllLines(@"your output file path", result);
谁把谁当真 2024-12-07 07:19:29

似乎不是最短的方法...但它对我有用...希望它提供一些见解。

        System.IO.StreamReader input = new System.IO.StreamReader(@"originalFile.txt");
        System.IO.StreamWriter output = new System.IO.StreamWriter(@"outputFile.txt");

        String[] allLines = input.ReadToEnd().Split("\n".ToCharArray());

        int numOfLines = allLines.Length;
        int lastLineWeWant = numOfLines - (6);                  //The last index we want. 

        for (int x = 0; x < numOfLines; x++)
        {
            if (x > 4 - 1 && x < lastLineWeWant)  //Index has to be greater than num to skip @ start and below the total length - num to skip at end.
            {
                output.WriteLine(allLines[x].Trim());  //Trim to remove any \r characters.
            }
        }

        input.Close();
        output.Close();

Doesn't appear to be the shortest way... but it works for me... Hope it provides some insight.

        System.IO.StreamReader input = new System.IO.StreamReader(@"originalFile.txt");
        System.IO.StreamWriter output = new System.IO.StreamWriter(@"outputFile.txt");

        String[] allLines = input.ReadToEnd().Split("\n".ToCharArray());

        int numOfLines = allLines.Length;
        int lastLineWeWant = numOfLines - (6);                  //The last index we want. 

        for (int x = 0; x < numOfLines; x++)
        {
            if (x > 4 - 1 && x < lastLineWeWant)  //Index has to be greater than num to skip @ start and below the total length - num to skip at end.
            {
                output.WriteLine(allLines[x].Trim());  //Trim to remove any \r characters.
            }
        }

        input.Close();
        output.Close();
深海蓝天 2024-12-07 07:19:29

StreamReader.ReadLine() 逐行读取文件,您可以从文件构建字符串数组。然后从数组中删除前 4 行和后 6 行。
使用 StreamWriter.WriteLine(),您可以从数组中逐行填充新文件。应该很简单。

StreamReader.ReadLine() reads file line by line and you can build an array of strings from the file. Then delete first four and last 6 lines from the array.
And with StreamWriter.WriteLine() you can populate new file line by line taking from your array. Should be quite simple.

愛上了 2024-12-07 07:19:29

这是在 VB.NET 中执行此操作的最简单方法:

Private Sub ReplaceString()
    Dim AllLines() As String = File.ReadAllLines("c:\test\myfile.txt")
    For i As Integer = 0 To AllLines.Length - 1
        If AllLines(i).Contains("foo") Then
            AllLines(i) = AllLines(i).Replace("foo", "boo")
        End If
    Next
    File.WriteAllLines("c:\test\myfile.txt", AllLines)
End Sub

Here is the easiest way to do it in VB.NET:

Private Sub ReplaceString()
    Dim AllLines() As String = File.ReadAllLines("c:\test\myfile.txt")
    For i As Integer = 0 To AllLines.Length - 1
        If AllLines(i).Contains("foo") Then
            AllLines(i) = AllLines(i).Replace("foo", "boo")
        End If
    Next
    File.WriteAllLines("c:\test\myfile.txt", AllLines)
End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文