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

本文关键字:文件 取文本 读取 然后 文本 另一个 应用程序 控制台 一个 | 更新日期: 2023-09-27 18:01:41

 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文件流。

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

下面的代码可以满足您的要求:

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数据接口的完美库时,我对直接写入文件的XML感到畏缩。您可能还应该寻找一个合适的CSV解析器,或者自己编写一个;这不是一个很难匹配的规范