【问题标题】:How do I read from all files in a given folder?如何读取给定文件夹中的所有文件?
【发布时间】:2011-07-22 12:50:38
【问题描述】:

下面的程序解析Dwarf Fortress.的所谓RAW文件之一代码工作正常,但在我当前的实现中,我需要为每个文件运行一次程序,每次手动更改源文件。有没有办法可以解析给定文件夹中的所有文本文件?

(请注意,当前输出文件与输入文件位于同一文件夹中。我不太确定如果程序试图打开它正在写入的文件会发生什么,但它是一些东西记住)。

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // create reader & open file
            string filePath = @"C:\foo\Dwarf Fortess\creature_domestic.txt";
            string destPath = @"C:\foo\Dwarf Fortess\eggCreatures.txt";
            string line;
            // 
            TextReader tr = new StreamReader(filePath);
            TextWriter tw = new StreamWriter(destPath);

            // read a line of text
            while ((line = tr.ReadLine()) != null) 
            {

                        if (line.Contains("[CREATURE:"))
                        {
                            tw.WriteLine(line);
                        }
                        if(line.Contains("[LAYS_EGGS]"))
                        {
                            tr.ReadLine();
                            tr.ReadLine();
                            tr.ReadLine();
                            tw.WriteLine(tr.ReadLine());
                            tw.WriteLine(tr.ReadLine());
                        }


            }

            // close the stream
            tr.Close();
            tw.Close();
        }
    }
}

【问题讨论】:

    标签: c#


    【解决方案1】:

    什么版本的.Net?您可以对 Directory.GetFiles 使用 linq 来排除您正在写入的文件。我现在没有 VS,可能有一些错误,但希望你能明白。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string destPath = @"C:\foo\Dwarf Fortess\eggCreatures.txt";
                FileInfo fiDest = new FileInfo(destPath);
                DirectoryInfo diDF = new DirectoryInfo(@"C:\foo\Dwarf Fortress");
    
                var files = from FileInfo f in diDF.GetFiles("*.txt")
                    where f.Name != fiDest.Name
                    select f
                foreach(FileInfo fiRead in files) 
                {
    
                // create reader & open file
                    string filePath = @"C:\foo\Dwarf Fortess\creature_domestic.txt";
                    string line;
                    // 
                    TextReader tr = fiRead.OpenText();
                    TextWriter tw = new StreamWriter(destPath);
    
                    // read a line of text
                    while ((line = tr.ReadLine()) != null) 
                    {
    
                                if (line.Contains("[CREATURE:"))
                                {
                                    tw.WriteLine(line);
                                }
                                if(line.Contains("[LAYS_EGGS]"))
                                {
                                    tr.ReadLine();
                                    tr.ReadLine();
                                    tr.ReadLine();
                                    tw.WriteLine(tr.ReadLine());
                                    tw.WriteLine(tr.ReadLine());
                                }
    
    
                    }
    
                    // close the stream
                    tr.Close();
                    tw.Close();
                }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以使用Directory.EnumerateFiles 获取目录中的所有文件并循环访问它们。您可以提供搜索规范以及搜索是否应该是递归的,具体取决于您使用的参数和重载。

      顺便说一句 - 您应该将流包装在 using 语句中以确保正确处理:

      using(TextReader tr = new StreamReader(filePath))
      {
        using(TextWriter tw = new StreamWriter(destPath))
        {
          ....
        }
      }
      

      【讨论】:

      • using 让我知道 .Close() 没有什么?
      • @Raven Dreamer - 如果在调用 Close 之前引发异常,则使用您的代码不会关闭流。
      • 啊,所以我也可以在最后关闭它们。明白了。
      • @Raven Dreamer - 是的。这几乎就是using 为您所做的。一点语法糖。
      【解决方案3】:

      检查Directory.EnumerateFiles 它列出了目录中的所有文件。您可以使用 options 参数选择它是否以递归方式工作。

      然后简单地加载它给你的文件。

      【讨论】:

        猜你喜欢
        • 2011-08-15
        • 2010-12-23
        • 1970-01-01
        • 2021-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多