C#File类

File类:静态类,专门操作文件的类,仅对小文件可以,对大文件要使用文件流。

[File 命名空间]using System.IO; 

[Encoding 命名空间]using System.Text;

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建一个文件
            File.Create(@"C:\Users\SpringRain\Desktop\new.txt");
            Console.WriteLine("创建成功");
            Console.ReadKey();

            //删除一个文件
            File.Delete(@"C:\Users\SpringRain\Desktop\new.txt");
            Console.WriteLine("删除成功");
            Console.ReadKey();

            //复制一个文件
            File.Copy(@"C:\Users\SpringRain\Desktop\code.txt", @"C:\Users\SpringRain\Desktop\new.txt");
            Console.WriteLine("复制成功");
            Console.ReadKey();

            //剪切一个文件
            File.Move(@"C:\Users\SpringRain\Desktop\code.txt", @"C:\Users\SpringRain\Desktop\newnew.txt");
            Console.WriteLine("剪切成功");
            Console.ReadKey();

            //读取整个文件并解码送显
            byte[] buffer = File.ReadAllBytes(@"C:\Users\SpringRain\Desktop\抽象类特点.txt");
            //将字节数组中的每一个元素都要按照我们指定的编码格式解码成字符串
            string s = Encoding.Default.GetString(buffer);
            string ss = Encoding.GetEncoding("GB2312").GetString(buffer);
            Console.WriteLine(s);
            Console.ReadKey();

            //将字符串写入到文本文件中
            //没有这个文件的话 会给你创建一个 有的话 会给你覆盖掉
            string str = "今天天气好晴朗处处好风光";
            //需要将字符串转换成字节数组
            byte[] buffer1 = Encoding.Default.GetBytes(str);
            File.WriteAllBytes(@"C:\Users\SpringRain\Desktop\new.txt", buffer1);
            Console.WriteLine("写入成功");
            Console.ReadKey();

            //按行读取文本文件中的文本,返回值为字符串数组
            string[] contents = File.ReadAllLines(@"C:\Users\SpringRain\Desktop\抽象类特点.txt", Encoding.Default);
            foreach (string item in contents)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();

            //读取文本文件中的文本,返回值为字符串
            string str1 = File.ReadAllText("抽象类特点.txt", Encoding.Default);
            Console.WriteLine(str1);
            Console.ReadKey();

            //按字节数组将字符串写入文本文件中
            string str2 = "今天是最后一天复习啦!";
            byte[] buffer2 = Encoding.Default.GetBytes(str2);
            File.WriteAllBytes(@"C:\Users\SpringRain\Desktop\new.txt", buffer2);
            Console.WriteLine("OK");
            Console.ReadKey();

            //按行将字符串数组写入文本文件
            File.WriteAllLines(@"C:\Users\SpringRain\Desktop\new.txt", new string[] { "aoe", "ewu" });
            Console.WriteLine("OK");
            Console.ReadKey();

            //将字符串写入到文本文件
            File.WriteAllText(@"C:\Users\SpringRain\Desktop\new.txt", "张三李四王五赵六");
            Console.WriteLine("OK");
            Console.ReadKey();

            //将字符串追加到文本文件
            File.AppendAllText(@"C:\Users\SpringRain\Desktop\new.txt", "看我有木有把你覆盖掉");
            Console.WriteLine("OK");
            Console.ReadKey();
        }
    }
}


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值