C#读取/写入Text

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace OperateTextFile
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = AppDomain.CurrentDomain.BaseDirectory + "test.txt";
            FileStream fs = new FileStream(path, FileMode.Create);
            fs.Close();

            WriteText("血型=O型",path,true);
            WriteText("身高=180", path, true);
            WriteText("名字=小王", path, true);
            WriteText("他妹妹的名字=小王", path, true);

            List<string> list = ReadText(path);
            WriteReplaceText(path, list, false, "名字=小王", "名字=小张");

            foreach (string s in ReadText(path))
            {
                Console.WriteLine(s);
            }
            Console.ReadKey();
        }
        public static List<string> ReadText(string path)
        {
            List<string> list = new List<string>();
            StreamReader sr = new StreamReader(path, Encoding.Default);
            while (!sr.EndOfStream)
            {
                string curstr = sr.ReadLine();
                list.Add(curstr);
            }
            sr.Close();
            return list;
        }
        public static void WriteText(List<string> list,string path,bool append)
        {
            StreamWriter sw = new StreamWriter(path, append, Encoding.Default);
            foreach (string str in list)
            {
                sw.WriteLine(str);
            }
        }
        public static void WriteText(string str, string path, bool append)
        {
            StreamWriter sw = new StreamWriter(path, append, Encoding.Default);
            sw.WriteLine(str);
            sw.Close();
        }
        public static void WriteReplaceText(string path,List<string> list,bool append,string oldstr,string newstr)
        {
            StreamWriter sw = new StreamWriter(path, append, Encoding.Default);
            foreach (string str in list)
            {
                string temp = "";
                temp=str.Replace(oldstr, newstr);
                sw.WriteLine(temp);
            }
            sw.Close();
        }
    }
}


 

问题帖:
http://bbs.csdn.net/topics/390274556?page=1#post-392861996
读取文本文件修改,感觉还是SQL方便.郁闷...OPENDATASOURCE 读取后.再BCP 导出一下即可.

相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页