【发布时间】: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#