c# path类与File类 文件流
来源:互联网 发布:python 改变当前路径 编辑:程序博客网 时间:2024/05/18 08:56
1 Path类
static void Main(string[] args) { string str = @"C:\3000soft\Red Spider\Data\Message\老赵.wav"; //获得文件名 Console.WriteLine(Path.GetFileName(str)); //获得文件名但是不包含扩展名 Console.WriteLine(Path.GetFileNameWithoutExtension(str)); //获得文件的扩展名 Console.WriteLine(Path.GetExtension(str)); //获得文件所在的文件夹的名称 Console.WriteLine(Path.GetDirectoryName(str)); //获得文件所在的全路径 Console.WriteLine(Path.GetFullPath(str)); //连接两个字符串作为路径 Console.WriteLine(Path.Combine(@"c:\a\" , "b.txt")); Console.ReadKey(); }输出结果:
老赵.wav
老赵
.wav
C:\3000soft\Red Spider\Data\Message
C:\3000soft\Red Spider\Data\Message\老赵.wav
c:\a\b.txt
2 File类(只能用于读取小文件,如果想读取大文件需要用文件流来操作)
1)新建文件
File.Create(@"D:\Amazing\桌面\copy.txt");
2)删除文件
File.Delete(@"D:\Amazing\桌面\copy.txt");
3)复制文件(new.tex文件若之前没有,会自动创建)
File.Copy(@"D:\Amazing\桌面\copy.txt", @"D:\Amazing\桌面\new.txt");
4)剪切文件 (原文件copy.text被剪切为newnew.txt)
File.Move(@"D:\queena.pan\桌面\copy.txt", @"D:\queena.pan\桌面\newnew.txt");
5)向文件中写入数据
(先将new.tex中的原有数据清空,再重新写入数据)
(若本来无new.tex文件,会新建一个,再写入数据)
string s = "今天天气好晴朗,处处好风光"; //将字符串转换成字节数组 byte[] buffer = Encoding.Default.GetBytes(s); //以字节的形式向计算机中写入文本文件 File.WriteAllBytes(@"D:\Amazing\桌面\new.txt", buffer); Console.WriteLine("写入成功"); Console.ReadKey();
6)使用File类来实现文件的读取/写入操作
1 文本/多媒体/音乐/视频等文件 ------ReadAllBytes/WriteAllBytes
(如果本身没有 copy.txt,会新建一个,如果本身有copy.txt会覆盖原有copy.txt的内容)
//读取 byte[] buffer = File.ReadAllBytes(@"D:\Amazing\桌面\new.txt"); //写入 File.WriteAllBytes(@"D:\Amazing\桌面\copy.txt", buffer); Console.WriteLine("复制成功"); Console.ReadKey();2 只能操作文本文件------ReadAllLines/WriteAllLInes(返回一个string数组) ReadAllText/WriteAllText(返回一个字符串string)
string[] contents = File.ReadAllLines(@"C:\Users\SpringRain\Desktop\抽象类特点.txt", Encoding.Default); foreach (string item in contents) { Console.WriteLine(item); } Console.ReadKey(); string str = File.ReadAllText("抽象类特点.txt", Encoding.Default); Console.WriteLine(str); 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();
3 AppendAllText/AppendAllLines 在原文件的数据尾端写入数据,而不覆盖原文件
File.AppendAllText(@"C:\Users\SpringRain\Desktop\new.txt", "看我有木有把你覆盖掉"); Console.WriteLine("OK"); Console.ReadKey();如果new.text原来是存的123456789,则调用File.AppendAllText后,输出结果为:123456789看我有木有把你覆盖掉
3 FileStream(文件流)
1)FileStream 操作字节,所有文件都可以读数据写数据
新建操作流:文件路径/文件类型/要进行的操作
FileStream fsRead = new FileStream(@"C:\Users\SpringRain\Desktop\new.txt", FileMode.OpenOrCreate, FileAccess.Read);
读操作:参数:1 读出的数据存放在byte[ ]数组中 2 从哪儿开始读 offset=0是从文件开始进行读 3 一次度多少字节
fsRead.Read(byte[] array, int offset, int count)
将字节数组中每一个元素按照指定的编码格式解码成字符串:
Encoding.UTF8.GetString(byte[] bytes, int index, int count);
需要手动关闭流和释放流资源,或者将创建文件流对象的过程写在using当中,以自动的帮助我们释放流所占用的资源。
//关闭流
fsRead.Close();
//释放流所占用的资源
fsRead.Dispose();
举例:
读数据;
//使用FileStream来读取数据 FileStream fsRead = new FileStream(@"C:\Users\SpringRain\Desktop\new.txt", FileMode.OpenOrCreate, FileAccess.Read); byte[] buffer = new byte[1024 * 1024 * 5]; //3.8M 5M //返回本次实际读取到的有效字节数 int r = fsRead.Read(buffer, 0, buffer.Length);//从文件开始(即参数offset=0)去读取,每次读取 buffer.Length长度 //将字节数组中每一个元素按照指定的编码格式解码成字符串 string s = Encoding.UTF8.GetString(buffer, 0, r); //关闭流 fsRead.Close(); //释放流所占用的资源 fsRead.Dispose(); Console.WriteLine(s); Console.ReadKey();
写数据:
将创建文件流对象的过程写在using当中,会自动的帮助我们释放流所占用的资源。
//使用FileStream来写入数据(会一个字节一个字节的将源文件的数据进行覆盖,没有覆盖到的源文件数据保留) using (FileStream fsWrite = new FileStream(@"C:\Users\SpringRain\Desktop\new.txt", FileMode.OpenOrCreate, FileAccess.Write)) { string str = "56565656"; byte[] buffer = Encoding.UTF8.GetBytes(str); fsWrite.Write(buffer, 0, buffer.Length); } Console.WriteLine("写入OK"); Console.ReadKey();如果new.txt中原来的数据为:111111111111,则写入后,会成为:565656561111
使用文件流来实现多媒体文件的复制
namespace _07使用文件流来实现多媒体文件的复制{ class Program { static void Main(string[] args) { //思路:就是先将要复制的多媒体文件读取出来,然后再写入到你指定的位置 string source = @"C:\Users\SpringRain\Desktop\1、复习.wmv"; string target = @"C:\Users\SpringRain\Desktop\new.wmv"; CopyFile(source, target); Console.WriteLine("复制成功"); Console.ReadKey(); } public static void CopyFile(string soucre, string target) { //1、我们创建一个负责读取的流 using (FileStream fsRead = new FileStream(soucre, FileMode.Open, FileAccess.Read)) { //2、创建一个负责写入的流 using (FileStream fsWrite = new FileStream(target, FileMode.OpenOrCreate, FileAccess.Write)) { byte[] buffer = new byte[1024 * 1024 * 5]; //因为文件可能会比较大,所以我们在读取的时候 应该通过一个循环去读取 while (true) { //返回本次读取实际读取到的字节数 //(比如文件统共33M,一次读取5M,那最后一次读取的r是3M.) int r = fsRead.Read(buffer, 0, buffer.Length); //如果返回一个0,也就意味什么都没有读取到,读取完了 if (r == 0) { break; } fsWrite.Write(buffer, 0, r); } } } } }}
2)StreamReader/StreamWriter 操作字符,只能操作文本文件,但可以操作大文件
新建StreamReader :StreamReader sr = new StreamReader(string path, Encoding encoding);(有很多重载,可以自由选择)
新建StreamWriter :StreamWriter sw = new StreamWriterStreamWriter(string path, bool append);(将最后一个参数设置为true可避免原文件数据被覆盖)
class Program { static void Main(string[] args) { //使用StreamReader来读取一个文本文件 using (StreamReader sr = new StreamReader(@"C:\Users\SpringRain\Desktop\抽象类特点.txt",Encoding.Default)) { while (!sr.EndOfStream) { Console.WriteLine(sr.ReadLine()); } } //使用StreamWriter来写入一个文本文件 using (StreamWriter sw = new StreamWriter(@"C:\Users\SpringRain\Desktop\newnew.txt",true)) { sw.Write("看我有木有把你覆盖掉"); } Console.WriteLine("OK"); Console.ReadKey(); } }
- c# path类与File类 文件流
- 【C#基础知识】——File(文件操作)Path类
- Path 路径类 | Directory 文件夹类 | File 文件类 | FileStream 文件流类----------StreamReaded与StreamWriter
- C#文件与流(FileStream、StreamWriter 、StreamReader 、File、FileInfo、Directory、DirectoryInfo、Path、Encoding)
- C#文件操作常用相关类(Directory类、File类、Path类)
- C# Path操作路径文件、File处理文件、读写文件:byte[];string[];string; string与byte[]转换,File.AppendAllText()
- C#中Path类、File类、Directory类总结
- C#基础精华04(文件流,文件操作,File、Directory、Path,Directory)
- C# 文件路径 Path类 测试
- Path类和File类
- Path类和File类
- C#中File类的文件操作方法
- C# File类,文件读取,编码
- 文件与流(FileStream、StreamWriter 、StreamReader 、File、FileInfo、Directory、DirectoryInfo、Path、Encoding)
- 文件File path与content:// Uri的相互转换
- 文件File path与content:// Uri的相互转换
- C#基础(19)——path、File类的基本操作
- file类的遍历文件与目录
- swoole linux 安装
- HTML 解析器
- 小朋友学Java(8):重写与重载
- 利用Node.js进行移动物联网的嵌入式进行快速开发
- effective C++ 07_为多态基类声明virtual析构函数 读书笔记
- c# path类与File类 文件流
- mysql 命令导出导入文件
- chrome打开网站视频显示no cimpatable source was found for this media
- IT风投兴起原因
- linux下搭建svn服务器
- spark 运行模式的简单总结
- 小朋友学Java(9):抽象类与接口
- ali-tomcat部署war包去掉工程名
- js:正则判断手机号是否正确