• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

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

武飞扬头像
随心zz
帮助1

个人向记录,便于复习。

重点:使用流对文件进行操作时,文件会被锁住,使用完后一定要用Close()方法关闭流,释放资源,否则当别的方法调用该文件进行操作时,该文件会因为被锁住而无法操作。

使用using关键字,会自动释放资源,无需再调用Close()方法。

  1.  
    using System;
  2.  
    using System.IO;
  3.  
    using System.Text;
  4.  
    using System.Text.RegularExpressions;
  5.  
     
  6.  
    namespace ConsoleApp3
  7.  
    {
  8.  
    /*
  9.  
    城春草木深,城春草木深。
  10.  
    感时花溅泪,恨别鸟惊心。
  11.  
    烽火连三月,家书抵万金。
  12.  
    白头搔更短,浑欲不胜簪。
  13.  
    */
  14.  
    class Program
  15.  
    {
  16.  
    static void Main(string[] args)
  17.  
    {
  18.  
    //从上往下每个方法单独调试即可
  19.  
    //FileWrite(@"C:\Users\luzp\Desktop\file/text.txt", "会当凌绝顶,一览众山小。");
  20.  
    //FileRead(@"C:\Users\luzp\Desktop\file/text.txt");
  21.  
    //FileDetails(@"C:\Users\luzp\Desktop\file/text.txt");
  22.  
    //FileDelete(@"C:\Users\luzp\Desktop\file/text.txt");
  23.  
    FileMove(@"C:\Users\luzp\Desktop\file", @"C:\Users\luzp\Desktop", "123.txt");
  24.  
    Console.ReadKey();
  25.  
    }
  26.  
    //文件内容的写入
  27.  
    public static void FileWrite(string path,string content)
  28.  
    {
  29.  
    if (!File.Exists(path))
  30.  
    {
  31.  
    Console.WriteLine("文件不存在,创建“text.txt”文件");
  32.  
     
  33.  
    using (StreamWriter sw = File.CreateText(path))
  34.  
    {
  35.  
    sw.Write(content);
  36.  
    //sw.Close(); //uisng会自动释放资源
  37.  
    }
  38.  
     
  39.  
    Console.WriteLine("创建文件且写入内容成功!");
  40.  
    }
  41.  
    else
  42.  
    {
  43.  
    //单纯的写入,会清空文件原内容,再写入
  44.  
    //using (StreamWriter sw = new StreamWriter(path))
  45.  
    //{
  46.  
    // sw.Write(content);
  47.  
    //}
  48.  
     
  49.  
    //追加内容,在原文内容的基础上追加内容,与上面单纯的写入不同,自己单个调试品味区别
  50.  
    FileStream fs = new FileStream(path, FileMode.Append);
  51.  
    StreamWriter sw1 = new StreamWriter(fs);
  52.  
    sw1.Write(content); //Write在原内容基础上另取一行,WriteLine在原文最末端接上内容
  53.  
    sw1.Close(); //创建流使用后一定要记得关闭流,释放资源。
  54.  
    fs.Flush();
  55.  
     
  56.  
    Console.WriteLine("文件写入内容成功!");
  57.  
    }
  58.  
     
  59.  
    }
  60.  
    //文件内容的读取
  61.  
    public static void FileRead(string path)
  62.  
    {
  63.  
    if (!File.Exists(path))
  64.  
    {
  65.  
    Console.WriteLine("文件不存在!请检查路径!");
  66.  
    }
  67.  
    else
  68.  
    {
  69.  
    string []str = File.ReadAllLines(path); //遍历一行一行读取
  70.  
    foreach (string text in str)
  71.  
    {
  72.  
    Console.WriteLine(text);
  73.  
    }
  74.  
    Console.WriteLine("===================");
  75.  
    StreamReader sr = new StreamReader(path,Encoding.UTF8); //全部读取
  76.  
    string content = sr.ReadToEnd();
  77.  
    sr.Close();
  78.  
    Console.WriteLine(content);
  79.  
    }
  80.  
    }
  81.  
    //文件内容修改
  82.  
    public static void FileDetails(string path)
  83.  
    {
  84.  
    if (!File.Exists(path))
  85.  
    {
  86.  
    Console.WriteLine("路径不存在");
  87.  
    }
  88.  
    else
  89.  
    {
  90.  
    //string[] str = File.ReadAllLines(path);
  91.  
    string str = File.ReadAllText(path);
  92.  
    str = Regex.Replace(str,"国破山河在","城春草木深"); //将文件内所有的"国破山河在"替换为"城春草木深"
  93.  
    File.WriteAllText(path,str);
  94.  
    Console.WriteLine("文件内容修改成功");
  95.  
     
  96.  
    }
  97.  
    }
  98.  
    //文件内容删除
  99.  
    public static void FileDelete(string path)
  100.  
    {
  101.  
    File.WriteAllText(path, string.Empty); //string.Empty 等价于 null
  102.  
    Console.WriteLine("文件内容删除成功");
  103.  
    }
  104.  
     
  105.  
    //移动文件
  106.  
    public static void FileMove(string oldPath, string newPath,string fileName)
  107.  
    {
  108.  
    DirectoryInfo dirInfo = new DirectoryInfo(oldPath);
  109.  
    FileInfo []fi =dirInfo.GetFiles();
  110.  
    fi[0].MoveTo(newPath "/" fileName);
  111.  
    Console.WriteLine("文件移动成功");
  112.  
     
  113.  
    }
  114.  
     
  115.  
    }
  116.  
    }
学新通

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhfiifhh
系列文章
更多 icon
同类精品
更多 icon
继续加载