跳转到主内容
websoft网络软件专家 - 深耕网络技术,打造实用软件!

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 contents = new List() { "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 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() { "dsad", "dsad" }); } `` 三。

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

下一次我们将会去看看怎么以及如何操作好流进行我们的IO操作!

相关文章