fFileStreams的异常行为 [英] Very strange behavior with fFileStreams

查看:85
本文介绍了fFileStreams的异常行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我编写的一个简单程序,用于演示尝试循环写入多个文件时遇到的问题.这是我运行并产生问题的确切代码.

This is a simple program I wrote to demonstrate the problem I am having any time I try to write to multiple files in a loop. This is the exact code I have run and produced the problem with.

using System;
using System.Collections.Generic;
using System.IO;

namespace ConsoleApplication1
{
   class Program
    {
        static List<string> fileNames = new List<string>();

        static void Main()
        {
            Directory.CreateDirectory(@"C:\\textfiles");
            fileNames.AddRange(new string[] { @"1.txt", @"2.txt", @"3.txt" });
            WriteFiles();
        }

        static void WriteFiles()
        {
            foreach (string fileName in fileNames)
            {
                using (StreamWriter sw = new StreamWriter(File.Create(@"c:\\textfiles\\" + fileName)))
                {
                    sw.Write(@"This is my text");
                }
            }
        }
    }
}


执行完此操作后,我现在在C:\ textfiles文件夹中有3个文本文件(1.txt,2.txt,3.txt),以前都不存在.

当我在记事本中打开文件时,这就是我

1.txt-这是我的文字这是我的文字这是我的文字"

2.txt-什么都没有

3.txt-没什么

我很困惑为什么它要创建所有三个文件,却要全部写入三个文件.难道不应该每次写入它创建的新文件吗?

更奇怪的是,如果我不使用循环,就像这样


After executing this, I now have 3 text files (1.txt, 2.txt, 3.txt) in the folder C:\textfiles, none of which existed before.

When I open the files in notepad here''s what I''ve got

1.txt - "This is my textThis is my textThis is my text"

2.txt - nothing

3.txt - nothing

I am puzzled as to why it is creating all three files, but writing to the first one all three times. Isn''t is supposed to write to the new file it creates every time?

What''s even more strange is if I dont use a loop, like this

using System;
using System.Collections.Generic;
using System.IO;

namespace ConsoleApplication1
{
   class Program
    {
        static void Main()
        {
            Directory.CreateDirectory(@);
            WriteFiles();
        }

        static void WriteFiles()
        {
            using (StreamWriter sw1 = new StreamWriter(File.Create(@"C:\\textfiles\\1.txt")))
            {
                sw1.Write(@"This is my text");
            }

            using (StreamWriter sw1 = new     StreamWriter(File.Create(@"C:\\textfiles\\2.txt")))
            {
                sw2.Write(@"This is my text");
            }

            using (StreamWriter sw1 = new StreamWriter(File.Create(@"C:\\textfiles\\3.txt")))
            {
                sw3.Write(@"This is my text");
            }
        }
    }
}



它工作正常.我在每个文件中都写了一次这是我的文字".

但是有时我必须使用循环,因为在运行时之前我不知道要写入多少文件.

我是不是很愚蠢,在这里缺少明显的东西?



It works fine. I have "This is my text" written once in each file.

But sometimes I have to use a loop, because I won''t know what/how many files to write to until runtime.

Am I stupid and missing something obvious here?

推荐答案

引起我注意的第一件事是File.Create.您不需要它.原因如下:
First thing which caught my eye is File.Create. You don''t need it. Here is why:
MSDN help page on StreamWriter(string path) constructor says:

如果该文件存在,则将其覆盖;否则,该文件将被覆盖.否则,将创建一个新文件.

If the file exists, it is overwritten; otherwise, a new file is created.

请先尝试.

—SA

Please try it first.

—SA


尝试显式打开和关闭流,而不是将其包装在使用中".
Try to open and close the stream explicitly instead of wrapping it inside of "using."


这篇关于fFileStreams的异常行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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