简体   繁体   English

如何将一个文本文件的内容复制到另一个长度减小的文件中?

[英]How to copy contents of one text file to another with reduced length?

I have a text file with about 5,000 lines and I want to copy its contents to another file, but only the first 38 characters of each line. 我有一个约有5,000行的文本文件,我想将其内容复制到另一个文件中,但是每行仅前38个字符。

I currently have this code: 我目前有以下代码:

private void button1_Click(object sender, EventArgs e)
    {
        string line, line2;

        System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt");
        while ((line = file.ReadLine()) != null)
        {
            line2 = line.Substring(0, 38);
            using (System.IO.StreamWriter files = new System.IO.StreamWriter(@"C:\test2.txt"))
            {
                files.WriteLine(line2);
            }
        }

        file.Close();
    }

It only copies the last line. 它仅复制最后一行。 :( :(

because you rewrite your new file in your loop. 因为您在循环中重写了新文件。 You should create your new string in the loop (use a stringBuilder for this will be more efficient), but write the new file out of the loop : 您应该在循环中创建新的字符串(使用stringBuilder可以提高效率),但是请在循环外写入新文件:

    string line;
    var sb = new StringBuilder();
    System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt");
    while ((line = file.ReadLine()) != null)
        sb.AppendLine(line.Substring(0, Math.Min(38, line.Length)));

    file.Close();
    using (System.IO.StreamWriter files = new System.IO.StreamWriter(@"C:\test2.txt"))
        {
            files.WriteLine(sb.ToString());
        }

or to do it shorter 或更短一点

var result = File.ReadAllLines(@"c:\test.txt")
                 .Select(m => m.Substring(0, Math.Min(38, m.Length)));
File.WriteAllLines(@"C:\test2.txt", result);

You need to move the creation of 'file2' before the while loop. 您需要在while循环之前移动“ file2”的创建。 You should also create 'file' in a using:. 您还应该在using:中创建“文件”。 You won't need to call close for either one then. 然后,您无需为任何一个调用close。

private void button1_Click(object sender, EventArgs e)
{
    string line;

    using (System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt"))
        using (System.IO.StreamWriter file2 = new System.IO.StreamWriter(@"C:\test2.txt"))
            while ((line = file.ReadLine()) != null)
            {
                string line2 = line.Substring(0, 38);
                file2.WriteLine(line2);
            }
}

Your file's .WriteLine overwrites the entire file every time you call it. 每次调用时,文件的.WriteLine覆盖整个文件。 Therefore, put the entire code in your using block, or add true to the StreamWriter 's arguments to tell it to append to the file instead of overwriting. 因此,将整个代码放在using块中,或将true添加到StreamWriter的参数中以告诉它追加到文件中而不是覆盖。

Option 1: 选项1:

private void button1_Click(object sender, EventArgs e)
{
    string line, line2;
    using (System.IO.StreamWriter files = new System.IO.StreamWriter(@"C:\test2.txt"))
        {
        System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt");
        while ((line = file.ReadLine()) != null)
        {
            line2 = line.Substring(0, 38);

            files.WriteLine(line2);

        }

        file.Close();
    }
}

Option 2: 选项2:

private void button1_Click(object sender, EventArgs e)
    {
        string line, line2;

        System.IO.StreamReader file = new System.IO.StreamReader(@"c:\test.txt");
        while ((line = file.ReadLine()) != null)
        {
            line2 = line.Substring(0, 38);
            using (System.IO.StreamWriter files = new System.IO.StreamWriter(@"C:\test2.txt",true))
            {
                files.WriteLine(line2);
            }
        }

        file.Close();
    }

And finally, if you choose to use a StringBuilder, you can use System.IO.File.WriteAllText(@"C:\\test2.txt", stringHere); 最后,如果选择使用StringBuilder,则可以使用System.IO.File.WriteAllText(@"C:\\test2.txt", stringHere); instead of the entire using and StreamWriter block 而不是整个usingStreamWriter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM