当前位置: 移动技术网 > IT编程>开发语言>c# > 文件操作及编码格式

文件操作及编码格式

2019年11月03日  | 移动技术网IT编程  | 我要评论
文件操作的相关类所在的命名空间在System.IO中File 操作文件,对文件整体操作,拷贝,删除,剪切等等Directory 操作目录(文件夹)静态类Path 对文件或目录的路径进行操作(操作的是字符串本身)Stream 文件流 1.FileStream 文件流 MemoryStream 内存流 ...

文件操作的相关类
所在的命名空间在system.io中
file 操作文件,对文件整体操作,拷贝,删除,剪切等等
directory 操作目录(文件夹)静态类
path 对文件或目录的路径进行操作(操作的是字符串本身)
stream 文件流
    1.filestream 文件流 memorystream 内存流 networkstream 网络流
    2.streamreader 快速读取文本文件
    3.streamwriter 快速写入文本文件
    
path 类常用方法
    string changeextension(string path,string extension)    修改文件的后缀,只修改字符串层面,并没有真实给文件改名
    string combine(string path1,string path2)
    将两个路径合并成一个路径,可以方便解决不加斜线的问题,自动处理路径分割符的问题 string combine(@"c:\temp","a.jpg");
    string getdirectoryname(string path)
    得到文件的路径名(文件夹)
    string getextension(string path)
    得到文件的扩展名
    string getfilename(string path)
    得到文件路径的文件名部分
    string getfilenamewithoutextension(string path)
    得到除去扩展名的文件名
    string getfullfath(string path)
    得到文件的全路径,可以根据相对路径获取绝对路径
    
file 类
    file.create(string path)
    指定路径创建文件
    file.delete(string path)
    删除指定路径文件
    file.copy(string sourcefile,string destfile)
    复制文件
    file.move(string sourcefile,string destfile)
    剪切文件
    
    使用file类读数据
    1.file.readallbytes(string path);    
    console.writeline(encoding.default.getstring(file.readallbytes(@"d:\1.txt")));
    以字节类型读取文件 返回一个字节数组
    2.file.readalllines(string path);
    读取文件的所有行 返回一个字符串数组
    string[] str = file.readalllines(path,encoding.default);
            foreach (var tstr in str) {
                console.writeline(tstr);
            }
    3.file.readalltext(string path)
    读取文件的所有行,返回一个字符串
    console.writeline(file.readalltext(path,encoding.default));
    
    使用file类写数据
    1.file.writeallbytes(string path,byte[] buffer);
    以字节类型写入文件
    file.writeallbytes(@"d:\text.txt", encoding.default.getbytes("自律真的好难"));
    2.file.writealllines(string path,string[] str)
    写入文件的所有行
    file.writealllines(@"d:\text.txt", new string[] { "asdf", "sdfa" });
    字符串数组中的每个元素占一行
    3.file.writealltext(string paht,string content)
    写入文件的所有行
    file.writealltext(@"d:\text.txt", "现在的穷只是暂时的,不可能穷一辈子");

directory类
    directory类是一个静态类,这个类中的所有方法都是静态方法,需要使用类名调用
    常用的方法
    directory.createdirectory(string path)
    指定目录创建文件夹
    directory.delete(string path,true)
    删除指定目录  ture的参数是在指定目录中存在文件的时候确认删除
    directory.move(string path)
    剪切指定目录
    
    string[] directory.getfilename(string path)
    获得指定目录下的所有文件,返回一个string[]
    获得指定目录下的指定文件添加参数"*.mp3"
    
编码
    将字符串以怎么样的形式存储为二进制数据 常用的格式有ascii、gbk、关闭2312、utf-8

 

上面的都是自己总结的部分,可能有的地方总结的不是很准确,还有遗漏,以后慢慢补充

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网