如何保持打开多个StreamWriter句柄? [英] How do I keep open multiple StreamWriter handles?

查看:76
本文介绍了如何保持打开多个StreamWriter句柄?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个很大的数据块,希望将其中的数据解析为500个文本文件.

我的具体要求是我同时打开所有500个句柄. (我知道这是无效的,等等).

我为每个文本文件生成句柄并存储在列表中.

在课堂上,我会做:

I have a large block of data and wish to parse data from it into 500 text files.

My very specific request, is that I keep open all 500 handles all at the same time. (I know this is inefficent etc etc ).

I generate handles for each text file and store in a list.

At the class level I do:

public static List<Tuple<string, StreamWriter>> myHandle = new List<Tuple<string, StreamWriter>>();





foreach (string[] cntrId in sri2)
{
    string newPath = @"U:\me1;
    string newFile = @"U:\me1" + me2 + ".txt";

    // If the dir doesn't exist, create it.
    System.IO.Directory.CreateDirectory(newPath);
    // create a new file
    StreamWriter sw = new StreamWriter(newFile, false);
    myHandle.Add(new Tuple<string, StreamWriter>(newFile, sw));
    sw.Close();
}



我必须在其他位置关闭文件,sw已经存在,无法创建另一个文件.

因此,我这样做:



I must close the file here else, sw already exists and I cant create another.

Hence I do this:

myHandle.Add(new Tuple<string, StreamWriter>(newFile2, new StreamWriter(newFile2, false)));



避免创建sw对象.

然后在另一堂课中我称之为



which avoids creating the sw object.

then much later on in another class I call

getHandle.getHandleNow(@"U:\processedText\" + myDate + "\\" + thisId + "\\" + thisCId + "\\" + myDate + "_raw.txt").WriteLine(message.ToString());



方法在哪里:



where the method is:

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

namespace B2T
{
    public class getHandle
    {
        public static StreamWriter getHandleNow(string myPath)
        {
            StreamWriter temp = null;

            foreach(Tuple<string, StreamWriter> tmp in processStaticData.myHandle)
            {
                if (string.Compare(tmp.Item1, myPath) == 0)
                {
                    temp = tmp.Item2;
                    break;
                }
            }
            return (temp);
        }
    }
}





应该调用streamwriter将我感兴趣的数据写到我需要的文件中.

然后我得到消息:





which should call streamwriter on to write the data I am interested in to the file I require.

I then get the message:

The process cannot access the file ''U:\processedText\20110103\me1\me2\20110103_raw.txt'' because it is being used by another process.



我不知道为什么要使用此句柄-它不是.到目前为止,该句柄唯一发生的事情就是它已创建.

我试图避免的是每次我写一条消息时都必须打开和关闭每个文件流.由于所有打开和关闭操作,这就是我当前的工作代码,需要花费很长时间-因此IO只希望保持所有流保持打开状态.

真的卡住了!



I have no idea why this handle is being used - it isnt. The only thing that has happened to that handle so far is that it has been created.

What I am trying to avoid is having to open and close each file stream, each time I write a message. This is what my current working code does and it takes so long, due to all the open and closing - hence IO just wish to keep all the streams open.

Really stuck! Please can you help!

推荐答案

这是打开的文件(在磁盘上)-不是StreamWriter对象.

创建一个使用线程根据需要打开/关闭文件的对象.除非要写入或读取它们,否则请不要打开它们.它们不需要一直打开,实际上,这是一个危险的设计缺陷.当应用存在时,您可能会无意中使所有文件保持打开状态,然后任何应用都无法访问它们.

在我看来,您的应用程序需要完全重构.
It''s the FILE (on the disk) that''s open - NOT the StreamWriter object.

Create an object that uses a thread to open/close files as needed. Don''t open them until you want to write to or read from them. They do NOT need to be open all the time, and in fact, this is a dangerous design flaw. You could inadvertantly leave all of the files open when the app exists, and then no app can access them.

Sounds to me like your app needs to be completely refactored.


这篇关于如何保持打开多个StreamWriter句柄?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆