06:File类的常用静态方法.pdf

时间:2022-09-09 16:11:39
【文件属性】:
文件名称:06:File类的常用静态方法.pdf
文件大小:163KB
文件格式:PDF
更新时间:2022-09-09 16:11:39
File类方法 文本文件编码,文本文件有不同的存储方式,将字符串以什么样的形式保存为二进制,这个就是编码,UTF-8、ASCII、Unicode等,如果出现乱码一般就是编码的问题,文本文件相关的函数一般都有一个Encoding类型的参数,取得编码的方式:Encoding.Default、Encoding.UTF8、Encoding.GetEncoding("GBK") 输出Encoding.GetEncodings(),所有编码。什么是文本文件。拖到记事本中还能看得懂的就是文本文件,doc不是。 File类的常用静态方法: (FileInfo*) void AppendAllText(string path, string contents), 将文本contents附加到文件path中(如果文件不存在,则创建) bool Exists(string path)判断文件path是否存在 string[] ReadAllLines(string path) 读取文本文件到字符串数组中 string ReadAllText(string path) 读取文本文件到字符串中 void WriteAllText(string path, string contents)将文本contents保存到文件path中,会覆盖旧内容。 WriteAllLines(string path,string[] contents),将字符串数组逐行保存到文件path中,会覆盖旧内容。 File.Copy(“source”, “targetFileName”, true); //文件拷贝,true表示当文件存在时“覆盖”,如果不加true,则文件存在报异常。 File.Exists();//判断文件是否存在 File.Move(“source”, “target”);//移动(剪切),思考如何为文件重命名? File.Delete(“path”);//删除。如果文件不存在?不存在,不报错

网友评论