读取文件并将修改后的数据复制到其他文件

时间:2016-08-26 07:13:19

标签: c#

我在每个oterh下都有一些数字,如下:

71004
71006
71008
71026
71028

他们站在一个文本文件中。我想读取文本文件,然后修改文本文件,因此它变为:

71004 | 71006 | 71008 | 71026 | 71028 |

我这样试试:

 class Program
    {
        static void Main(string[] args)
        {
            string file1 = @"D:\Docs\ImportDataInNAV\ImportVendorNumbers.txt";
            using (StreamReader stream = File.OpenText(file1))
            {
                string s = String.Empty;
                while ((s = stream.ReadLine()) != null)
                {
                    foreach (var line in s)
                    {
                        Console.WriteLine(line);
                    }
                }
            }

        }
    }

谢谢

我这样试试:

  using (FileStream stream = File.OpenRead(@"D:\\Docs\\ImportDataInNAV\\ImportVendorNumbers.txt"))

            using (FileStream writeStream = File.OpenWrite("D:\\file2.txt"))
            {
                var output = string.Join("|", File.ReadLines(writeStream));
                BinaryReader reader = new BinaryReader(stream);
                BinaryWriter writer = new BinaryWriter(writeStream);

                // create a buffer to hold the bytes 
                byte[] buffer = new Byte[1024];
                int bytesRead;

                // while the read method returns bytes
                // keep writing them to the output stream
                while ((bytesRead = stream.Read(buffer, 0, 1024)) > 0)
                {
                    //writeStream.Write(buffer, 0, bytesRead) ;
                    File.WriteAllText(filepath, output);
                }



            }

2 个答案:

答案 0 :(得分:3)

  

我想读取文本文件,然后修改文本文件,所以它   变为:

     

71004 | 71006 | 71008 | 71026 | 71028 |

使用string.Join形成|分隔字符串。

var output -string.Join("|",File.ReadLines(filepath));

File.WriteAllText(filepath, output);

如果在结束之前需要输出concat |并在输出结束之前输出concat |,则不确定INSERT INTO schedule( pending_id, scheduled_time ) VALUES ( "477", TIME( STR_TO_DATE( "09/02/2016 12:33 PM", "%m/%d/%y %h:%i %p"))) 是否有意。

答案 1 :(得分:0)

如何运行你的代码 yourprograme.exe> outputfile.txt