IO基础操作(文件)

IO基础操作(文件)

一.基础知识:
File:用于创建,删除,复制,移动,打开文件的静态方法并协助创建FileStream对象
FileInfo
用于创建,删除,复制,移动,打开文件的实例对象并协助创建FileStream对象
FileMode
文件打开方式:create,createNew,Append,Open,OpenCreate
FileAcess:
文件访问权限:Read,Write,ReadWrite
二.基本操作:File&FileInfo
1.创建

 public void FileExample()
        {
            string path = @"d:\ABCD\TEST\a.txt";
            File.Create(path);  //生成文件流
            File.Create(path,1024);  //指定缓冲区大小
	FileInfo fileInfo = new FileInfo(path); 
            fileInfo.Create();//生成文件流
            fileInfo.CreateText();  //StreamWriter
        }

2.删除

     public void FileExample2()
        {
            string path = @"d:\ABCD\TEST\a.txt";
            File.Delete(path);
            FileInfo fileInfo = new FileInfo(path);
            fileInfo.Delete();//生成文件流
        }

3.复制:

 public void FileExample3()
        {
            string path = @"d:\ABCD\TEST\a.txt";
            string path2 = @"d:\ABCD\TEST2\b.txt";
            File.Copy(path, path2,true);  //表示是不是允许覆盖
            FileInfo fileInfo = new FileInfo(path);
            fileInfo.CopyTo(path2);
        }

4.移动:

  public void FileExample4()
        {
            string path = @"d:\ABCD\TEST\a.txt";
            string path2 = @"d:\ABCD\TEST2\b.txt";
            File.Move(path, path2);
            FileInfo fileInfo = new FileInfo(path);
            fileInfo.MoveTo(path2);
        }

5.追加:

string path = @"d:\ABCD\TEST\a.txt";
            List<string> contents = new List<string>() { "111", "222", "333" };
            File.AppendAllLines(path,contents,System.Text.Encoding.UTF8); 
            File.AppendAllText(path, "dsadlskajlds");
            StreamWriter streamWriter = File.AppendText(path);  //创建一个写入流
            streamWriter.Write("dsadsadsads");  //写入
            FileInfo fileInfo = new FileInfo(path);
            StreamWriter streamWriter1 = fileInfo.AppendText();
            streamWriter1.Write("dsadsad");  //写入

6.打开文件:

 public void FileExample6()
        {
            string path = @"d:\ABCD\TEST\a.txt";
            FileStream fileStreamRead = File.OpenRead(path);  //只读的 ,证明是线程共享的
            FileStream fileStreamWrite = File.OpenWrite(path); //只写的,私有
            StreamReader streamReader = File.OpenText(path);  //打开文本文件
            File.Open(path, FileMode.Open); //若不存在则出现异常 打开现有文件
            File.Open(path, FileMode.Create); //若不存在
            File.Open(path, FileMode.CreateNew); //创建新文件 存在则异常
            File.Open(path, FileMode.Append, FileAccess.Read); //存在则定位到文件的末尾不存在则创建,文件的访问权限为只读
            File.Open(path, FileMode.OpenOrCreate, FileAccess.ReadWrite); //存在就打开不存在就创建,文件的访问权限为读写
        }

7.读取:

 public void FileExample7()
        {
            string path = @"d:\ABCD\TEST\a.txt";
            FileStream fileStream = File.Open(path, FileMode.Create, FileAccess.ReadWrite); //若不存在
            IEnumerable<string> contents1 = File.ReadLines(path);
            //打开一个文本文件读取所有行并关闭该文件
            string contents2 = File.ReadAllText(path);
            //打开一个文本文件读取所有行并关闭该文件
            string[] contents3 = File.ReadAllLines(path);
            //打开一个二进制文件读取所有字节并关闭该文件
            byte[] contents4 = File.ReadAllBytes(path);
            string contents5 = System.Text.Encoding.UTF8.GetString(contents4);
        }

8.写入:

 public void FileExample8()
        {
            string path = @"d:\ABCD\TEST\a.txt";
            byte[] contents4 = File.ReadAllBytes(path);
            File.WriteAllText(path, "dsadsadsa");
            File.WriteAllBytes(path, contents4);//写入字节数组
            File.WriteAllLines(path, new List<string>() { "dsad", "dsad" });
        }

``
三。总结:
在我们日常的工作中大多都是使用的基于这种底层的东西封装的类进行IO的操作,但是我们还是要清楚的知道,我们为什么这样进行文件的操纵,以及如何进行文件的操作,首先我们需要做的其实就是将一个存在磁盘中的文件以流的方式进行打开以及规定他的访问权限以及打开方式等等,然后就可以进行相关的操作,最后进行流的关闭。下一次我们将会去看看怎么以及如何操作好流进行我们的IO操作!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值