C#文件读写操作实例

来源:互联网 发布:js检验 编辑:程序博客网 时间:2024/05/04 15:06
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Filechange
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"D:\movie\test.txt";
            //创建filestream 对象
            FileStream file = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
            //创建Streamreader进行字节读取
            StreamReader sr = new StreamReader(file);
            int code = sr.Read();
            string str = null;
            while (code != -1)
            {
                str += Convert.ToChar(code);
                code = sr.Read();
            }
            str += " ";
            sr.Close();
            string str2 = "pc";
            char[] chs2 = str.ToCharArray();
            for (int i = 0; i < str.Length - 1; i++)
            {
                if (str.Substring(i, 2) == str2)
                {
                    chs2[i + 2] = 'L';
                }
            }
            string str3 = new string(chs2);
            foreach (var item in str3)
                Console.Write(item);
            //写操作
            StreamWriter sw = new StreamWriter(path, false);
            sw.Write(str3);
            sw.Close();
            Console.ReadKey();
        }
    }
}

在C#中对文本文件的读写操作还是比较简单的,对于封装好的函数只要稍微注意下使用的方法就应该可以了。

0 0
原创粉丝点击