zoukankan      html  css  js  c++  java
  • C#操作文本文件 在行首和行尾追加文本

       起因:

            有七个超过50行的SQL语句。但是不能作为存储过程放在数据库中。这样在写到C#的后台逻辑中需要在每一个行追加SqlStr.Append(“  和 ");这样两个东西。

       经过:

           1.在控制台中操作,先将SQL语句复制到VS中,美化一下,将每一行都顶格,也就是保证首字母没有空格。

           2.将美化好的语句,复制到C:\Test.txt文件中。并且在同一目录下创建NewFile.txt文件,用来存放新文件。

           3.控制台中写入以下代码:

           4.执行,可以看到,在控制台中也显示了变化后的SQL语句了。然后去NewFile.txt可以去拷贝了。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.IO;
    using System.Collections;
     
    namespace FileOperation {
        class Program {
            static void Main(string[] args) {
                //FileStream fs = new FileStream(@"C:\carError.txt", FileMode.Open, FileAccess.ReadWrite);
                StreamReader objreader = new StreamReader(@"C:\Test.txt",Encoding.Default);
                StreamWriter sw = new StreamWriter(@"C:\NewFile.txt");
                string sline = "";
                ArrayList al = new ArrayList();
                while (sline!=null) {
                    sline = objreader.ReadLine();
                    if (sline != null) {
                        al.Add("strSql.Append(\" " +sline+"\");");
                    }
                }
                objreader.Close();
                foreach (string s in al) {
                    sw.WriteLine(s);
                    Console.WriteLine(s);
                }
                sw.Close();
                Console.ReadLine();
            }
        }
    }

        结果:

               1.成功完成追加。注意:在StreamReader中编码要注意,如果不加编码,输出会出现乱码。

               2.积累。

  • 相关阅读:
    Objective C中提供了线程同步和异常处理
    iOS singleton单例模式的实现
    转:IOS UITableView中行的操作
    Javascript 函数
    ios category类别的使用
    vmware Ubuntu非法关机后启动不起来
    C++ Socket编程步骤
    C/C++ 笔试、面试题目大汇总(转)
    Linux下基于C/C++的Socket编程基础
    C++经典面试题
  • 原文地址:https://www.cnblogs.com/UpThinking/p/1652221.html
Copyright © 2011-2022 走看看