具有最大行数的Writer txt文件

时间:2014-11-03 12:16:03

标签: c# algorithm oop

我想创建一个最多包含400行数的txt文件。如果文本文件达到最大行数,则将创建具有不同名称的新文件,如果再次达到限制,则应用相同的大小写。

我写了一个类'MaxLinesWriter',但它的工作速度非常慢,并不像原始流光一样快。

有人能帮助我吗?

public class MaxLinesWriter
{
    private int n = 0;
    public int MaxLines;
    public string NameFile;
    private string ConstNameFile;
    public int CounterOfLines;

    DateTime date = new DateTime();

    public MaxLinesWriter(string NameFileInput, int MaxLinesInput)
    {
        MaxLines = MaxLinesInput;
        ConstNameFile = NameFileInput;
        NameFile = ConstNameFile + date.Hour.ToString() + date.Minute.ToString() + date.Second.ToString() + date.Millisecond.ToString();
        CounterOfLines = 0;
    }

    public void WriteLine(object StringToWrite)
    {
        if (CounterOfLines < MaxLines)
        {
            StreamWriter writer = new StreamWriter(NameFile + ".txt", true);
            writer.WriteLine(StringToWrite);
            CounterOfLines++;
            writer.Close();
        }
        else
        {
            CounterOfLines = 1;
            date = date.AddMilliseconds(1);
            NameFile = ConstNameFile + date.Hour.ToString() + date.Minute.ToString() + date.Second.ToString() + date.Millisecond.ToString();
            StreamWriter writer = new StreamWriter(NameFile + ".txt");
            writer.WriteLine(StringToWrite);
            writer.Close();
        }
    }
}

3 个答案:

答案 0 :(得分:2)

如果在迭代中调用WriteLine()方法,则从迭代中初始化StreamWriter

public class MaxLinesWriter
{
    private int n = 0;
    public int MaxLines;
    public string NameFile;
    private string ConstNameFile;
    public int CounterOfLines;
    private StreamWriter writer;

    DateTime date = new DateTime();

    public MaxLinesWriter(string NameFileInput, int MaxLinesInput)
    {
        MaxLines = MaxLinesInput;
        ConstNameFile = NameFileInput;
        NameFile = ConstNameFile + date.Hour.ToString() + date.Minute.ToString() + date.Second.ToString() + date.Millisecond.ToString();
        CounterOfLines = 0;
        writer = new StreamWriter(NameFile + ".txt", true);
    }

    public void WriteLine(object StringToWrite)
    {
        if (CounterOfLines < MaxLines)
        {
            writer.WriteLine(StringToWrite);
            CounterOfLines++;

        }
        else
        {
            writer.Close();
            CounterOfLines = 1;
            date = date.AddMilliseconds(1);
            NameFile = ConstNameFile + date.Hour.ToString() + date.Minute.ToString() + date.Second.ToString() + date.Millisecond.ToString();
            writer = new StreamWriter(NameFile + ".txt");
            writer.WriteLine(StringToWrite);
        }
    }

    // Call it after your last data
    public void Close()
    {
         writer.Close();
    }
}

答案 1 :(得分:1)

@Ali Sepehri.Kh 编写器将在构造函数的末尾关闭。您应该将leaveOpen设置为true以避免这种情况。

顺便说一句,设置更大的缓冲区也应该有所帮助。

这应该有效(测试):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace TEST
{
    class Program
    {
        public class MaxLinesWriter
        {
            public int MaxLines;
            public string NameFile;
            private string ConstNameFile;
            public int CounterOfLines;
            private FileStream st;
            private StreamWriter writer;

            DateTime date = new DateTime();

            public MaxLinesWriter(string NameFileInput, int MaxLinesInput)
            {
                MaxLines = MaxLinesInput;
                ConstNameFile = NameFileInput;
                NameFile = ConstNameFile + date.Hour.ToString() + date.Minute.ToString() + date.Second.ToString() + date.Millisecond.ToString();
                CounterOfLines = 0;
                st = new FileStream(NameFile + ".txt", FileMode.CreateNew);
                writer = new StreamWriter(st, Encoding.UTF8, 10240, true);
            }

            public void WriteLine(object StringToWrite)
            {

                if (CounterOfLines < MaxLines)
                {
                    writer.WriteLine(StringToWrite);
                    CounterOfLines++;
                }
                else
                {
                    CounterOfLines = 1;
                    date = date.AddMilliseconds(1);
                    NameFile = ConstNameFile + date.Hour.ToString() + date.Minute.ToString() + date.Second.ToString() + date.Millisecond.ToString();

                    st = new FileStream(NameFile + ".txt", FileMode.CreateNew);
                    writer = new StreamWriter(st, Encoding.UTF8, 10240, true);

                    writer.WriteLine(StringToWrite);

                }
            }
        }
        static void Main(string[] args)
        {
            MaxLinesWriter mx = new MaxLinesWriter("test", 10000);

            for (int i = 0; i < 1000000; i++)
                mx.WriteLine("Hello World");
        }
    }
}

答案 2 :(得分:1)

这应该有效:

public class MaxLinesWriter
{
    private int n = 0;
    public int MaxLines;
    public string NameFile;
    private string ConstNameFile;
    public int CounterOfLines;
    private StreamWriter writer;
    DateTime date = new DateTime();

    public MaxLinesWriter(string NameFileInput, int MaxLinesInput)
    {
        MaxLines = MaxLinesInput;
        ConstNameFile = NameFileInput;
        NameFile = ConstNameFile + date.Hour.ToString() + date.Minute.ToString() + date.Second.ToString() + date.Millisecond.ToString();
        CounterOfLines = 0;
        writer = new StreamWriter(NameFile + ".txt", true);
    }

    public void WriteLine(object StringToWrite)
    {
        if (CounterOfLines < MaxLines)
        {
            writer.WriteLine(StringToWrite);
            CounterOfLines++;
            writer.Flush();
        }
        else
        {
            CounterOfLines = 1;
            date = date.AddMilliseconds(1);
            NameFile = ConstNameFile + date.Hour.ToString() + date.Minute.ToString() + date.Second.ToString() + date.Millisecond.ToString();
            writer = new StreamWriter(NameFile + ".txt", true);
            writer.WriteLine(StringToWrite);
            writer.Flush();
        }
    }
}