步步为营-14-文件操作

Posted 逍遥小天狼

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了步步为营-14-文件操作相关的知识,希望对你有一定的参考价值。

1 对文件的操作-File类

  常用的操作:

  File.Exis();判断文件是否存在

  File.Create ();创建

  File.Move();剪切

  File.Copy();复制

  File.Delete();删除

技术分享
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FileTest
{
    class Program
    {
        static void Main(string[] args)
        {
            //判断文件是否存在
            if (File.Exists("1.txt"))
            {
                //删除文件
                File.Delete("1.txt");
                //创建文件
                File.Create("2.txt");
            }
            else {
                File.Create("1.txt");
            }
            //文件赋值
            File.Copy(@"C:\Users\home\Desktop\宽带连接.txt",@"E:\asd.txt");
            //文件剪切
            File.Move(@"E:\asd.txt",@"C:\Users\home\Desktop\宽带连接2.txt");

        }
    }
}
File

  读取文件

  string [] str = File.ReadAllLines(@"C:\Users\1.txt");

  string str2 = File.ReadAllText(@"C:\Users\1.txt");

    通过字节数组转化成字符串

  byte [] buffer = File.ReadAllBytes(@"C:\Users\1.txt");

  string str = Encoding.UTF8.GetSstring(buffer);

  cw(str);

  写文件

  第一种方法

    string abc = "要写入的字符串";

    //1先将字符串转换为字节数组

    byte[] buffer = Encoding.Default.GetBytes(abc);

    File.WriteAllBytes(@"C:\Users\1.txt",buffer);

  第二种

  File.WriteAllLines(@"C:\Users\1.txt",new string[]{"a","b"});

  第三种

  File.WriteAllText(@"C:\Users\1.txt",new string[]{"c"});

File比较简单,适合操作小的文本文件

以上是关于步步为营-14-文件操作的主要内容,如果未能解决你的问题,请参考以下文章

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装

VSCode自定义代码片段14——Vue的axios网络请求封装

14.VisualVM使用详解15.VisualVM堆查看器使用的内存不足19.class文件--文件结构--魔数20.文件结构--常量池21.文件结构访问标志(2个字节)22.类加载机制概(代码片段

步步为营-15-文件夹的操作

VSCode自定义代码片段——git命令操作一个完整流程