如何从一个文本文件中读取然后在一个控制台应用程序中写入另一个?

时间:2015-07-09 06:47:57

标签: c# file-io console

 static void Main(string[] args)
    {


        FileInfo fileInfo = new FileInfo("C:\\Users\\Alameer Ashraf\\Desktop\\data binding and I Ostreaming\\ResultFile.txt");
        using (StreamWriter writer = fileInfo.CreateText())
        {
            FileInfo Teto = new FileInfo("C:\\Users\\Alameer Ashraf\\Desktop\\data binding and I Ostreaming\\WorkerTable.txt");
            foreach (string line in File.ReadLines("C:\\Users\\Alameer Ashraf\\Desktop\\data binding and I Ostreaming\\WorkerTable.txt"))
            {

                string[] columns = line.Split(',');

                Console.WriteLine("<Name>");
                Console.WriteLine(columns[0]);
                Console.WriteLine("<" + "/" + "Name" + ">");

                Console.WriteLine("<ID >");
                Console.WriteLine(columns[1]);
                Console.WriteLine("<" + "/" + "ID " + ">");

                Console.WriteLine("<Number>");
                Console.WriteLine(columns[2]);
                Console.WriteLine("<" + "/" + "Number " + ">");

                Console.WriteLine("<Job>");
                Console.WriteLine(columns[3]);
                Console.WriteLine("<" + "/" + "Job " + ">");
            }
            Console.ReadLine();
        } 

我没有做我所解释的事情? 请帮忙 我想从一个文件中读取,然后使用相同的应用程序将我正在阅读的内容写入另一个文件中,我可以这样做吗? 我想读取.csv文件并从中提取信息,然后使用IO文件流将该信息重写到同一控制台应用程序中的另一个文本文件中。

1 个答案:

答案 0 :(得分:2)

以下代码将执行您想要的操作:

class Program
{
        static void Main(string[] args)
        {
            using(StreamReader reader = new StreamReader(File.OpenRead("C:\\Users\\Alameer Ashraf\\Desktop\\data binding and I Ostreaming\\WorkerTable.txt")))
            {
                using (StreamWriter writer = new StreamWriter(File.Open ("C:\\Users\\Alameer Ashraf\\Desktop\\data binding and I Ostreaming\\ResultFile.txt", FileMode.Create)))
                { 
                    string line;
                    while((line = reader.ReadLine()) != null) {
                        string[] columns = line.Split(',');

                        WriteAndPrint(writer, "<Name>");
                        WriteAndPrint(writer, columns[0]);
                        WriteAndPrint(writer, "</Name>");

                        WriteAndPrint(writer, "<ID >");
                        WriteAndPrint(writer, columns[1]);
                        WriteAndPrint(writer, "</ID >");

                        WriteAndPrint(writer, "<Number>");
                        WriteAndPrint(writer, columns[2]);
                        WriteAndPrint(writer, "</Number>");

                        WriteAndPrint(writer, "<Job>");
                        WriteAndPrint(writer, columns[3]);
                        WriteAndPrint(writer, "</Job>");
                    }
                }
            }
            Console.ReadLine();
        }

        static void WriteAndPrint(StreamWriter writer, string line)
        {
                Console.WriteLine(line);
                writer.WriteLine(line);
        }
    }

虽然在.NET中存在用于连接XML数据的a perfectly good library时,我看到直接写入文件的XML似乎很畏缩。您也应该寻找合适的CSV解析器,或者自己编写一个;它不是一个非常难以匹配的规范。