C#对TXT文件内容进行增删改操作

using System;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
 
namespace ConsoleApp3
{
    
    
    /*
    城春草木深,城春草木深。
    感时花溅泪,恨别鸟惊心。
    烽火连三月,家书抵万金。
    白头搔更短,浑欲不胜簪。
     */
    class Program
    {
    
    
        static void Main(string[] args)
        {
    
    
            //从上往下每个方法单独调试即可
            //FileWrite(@"C:\Users\luzp\Desktop\file/text.txt", "会当凌绝顶,一览众山小。");
            //FileRead(@"C:\Users\luzp\Desktop\file/text.txt");
            //FileDetails(@"C:\Users\luzp\Desktop\file/text.txt");
            //FileDelete(@"C:\Users\luzp\Desktop\file/text.txt");
            FileMove(@"C:\Users\luzp\Desktop\file", @"C:\Users\luzp\Desktop", "123.txt");
            Console.ReadKey();
        }
        //文件内容的写入
        public static void FileWrite(string path,string content)
        {
    
    
            if (!File.Exists(path))
            {
    
    
                Console.WriteLine("文件不存在,创建“text.txt”文件");
 
                using (StreamWriter sw = File.CreateText(path))
                {
    
     
                    sw.Write(content);
                    //sw.Close();  //uisng会自动释放资源
                }
                
                Console.WriteLine("创建文件且写入内容成功!");
            }
            else
            {
    
    
                //单纯的写入,会清空文件原内容,再写入
                //using (StreamWriter sw = new StreamWriter(path))
                //{ 
                //    sw.Write(content);
                //}  
                
                //追加内容,在原文内容的基础上追加内容,与上面单纯的写入不同,自己单个调试品味区别
                FileStream fs = new FileStream(path, FileMode.Append);
                StreamWriter sw1 = new StreamWriter(fs);
                sw1.Write(content);     //Write在原内容基础上另取一行,WriteLine在原文最末端接上内容
                sw1.Close();            //创建流使用后一定要记得关闭流,释放资源。
                fs.Flush();
 
                Console.WriteLine("文件写入内容成功!");
            }
 
        }
        //文件内容的读取
        public static void FileRead(string path)
        {
    
    
            if (!File.Exists(path))
            {
    
    
                Console.WriteLine("文件不存在!请检查路径!");
            }
            else
            {
    
    
                string []str = File.ReadAllLines(path); //遍历一行一行读取
                foreach (string text in str)
                {
    
    
                    Console.WriteLine(text);
                }
                Console.WriteLine("===================");
                StreamReader sr = new StreamReader(path,Encoding.UTF8);   //全部读取
                string content = sr.ReadToEnd();
                sr.Close();
                Console.WriteLine(content);
            }
        }
        //文件内容修改
        public static void FileDetails(string path)
        {
    
    
            if (!File.Exists(path))
            {
    
    
                Console.WriteLine("路径不存在");
            }
            else
            {
    
    
                //string[] str = File.ReadAllLines(path);
                string str = File.ReadAllText(path);
                str = Regex.Replace(str,"国破山河在","城春草木深");   //将文件内所有的"国破山河在"替换为"城春草木深"
                File.WriteAllText(path,str);
                Console.WriteLine("文件内容修改成功");
 
            }
        }
        //文件内容删除
        public static void FileDelete(string path)
        {
    
    
            File.WriteAllText(path, string.Empty);  //string.Empty 等价于 null
            Console.WriteLine("文件内容删除成功");
        }
 
        //移动文件
        public static void FileMove(string oldPath, string newPath,string fileName)
        {
    
    
            DirectoryInfo dirInfo = new DirectoryInfo(oldPath);
            FileInfo []fi =dirInfo.GetFiles();
            fi[0].MoveTo(newPath+"/"+fileName);
            Console.WriteLine("文件移动成功");
            
        }
 
    }
}

猜你喜欢

转载自blog.csdn.net/weixin_45023328/article/details/131025037