C#文件读写
来源:互联网 发布:omg淘宝权在哪里直播 编辑:程序博客网 时间:2024/05/08 00:14
这篇文章是关于C#的文件读写的相关备注。
1)FileInfo类
该类要实例化之后才能使用,该类对象标示一个文件。
构造方法:直接使用文件路径就可以创建一个FileInfo对象,其中注意使用@能够让转义字符\保持原意,不要转义。
下面这个例子能够判断文件是否存在
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FileTask{ class Program { static void Main(string[] args) { FileInfo file = new FileInfo(@"F:\test.txt"); if (file.Exists) { Console.WriteLine("File Exist!"); } else { Console.WriteLine("Not Exist"); } Console.Read(); } }}
这个类和File类类似,但是有下面几个点要注意:
如果仅进行单一方法调用,可以使用静态File类上的方法,因为不必要实例化新对象。
如果要对一个文件进行多种操作,那么实例化FileInfo对象并且使用其方法更好一些,因为对象已经在文件系统上引用正确的文件,而静态类必须每次都寻找文件。
其中和FileInfo类似的类有DirectoryInfo类。
2)FileStream对象
FileStream对象标示指向磁盘或者网络路径上的文件的流,经常使用StreamReader或StreamWriter执行文件的读写操作,不过这两种读写方法是有区别的,FileStream操作的是字节数组,而StreamReader和StreamWriter类操作的是字符数组,所以对文本操作StreamReader和StreamWriter类比较方便。
因为FileStream是对字节数组进行操作的,所以我们要将字节数组转化为字符数组才能进行显示,这中间的转化需要使用到Decoder(解码器)或者Encoder(编码器)。
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FileTask{ class Program { static void Main(string[] args) { FileInfo file = new FileInfo(@"F:\test.txt"); if (file.Exists) { Console.WriteLine("File Exist!"); FileStream fs = file.OpenRead(); Byte[] byteData = new Byte[200]; Char[] charData = new Char[200]; try { fs.Read(byteData, 0, 200); } catch (Exception e) { Console.WriteLine(e.Message); } Decoder d = Encoding.UTF8.GetDecoder(); d.GetChars(byteData, 0, byteData.Length, charData, 0, true); Console.WriteLine(charData); } else { Console.WriteLine("Not Exist"); } Console.Read(); } }}
下面是写操作:
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FileTask{ class Program { static void Main(string[] args) { FileInfo file = new FileInfo(@"F:\test.txt"); if (file.Exists) { Console.WriteLine("File Exist!"); FileStream fs = file.OpenWrite(); Byte[] byteData = new Byte[200]; Char[] charData = "Hello Sirius!".ToCharArray(); try { Encoder e = Encoding.UTF8.GetEncoder(); e.GetBytes(charData, 0, charData.Length, byteData, 0, true); fs.Write(byteData, 0, byteData.Length); fs.Close(); } catch (Exception e) { Console.WriteLine(e.Message); } } else { Console.WriteLine("Not Exist"); } Console.Read(); } }}
上面关于编码器和解码器的使用是重点,我认为编码器是用来解码的,解码器是用来编码的。我晕。
3)StreamWriter
在文本编辑上,StreamWriter比上面的FileStream类要方便多了。
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FileTask{ class Program { static void Main(string[] args) { FileInfo file = new FileInfo(@"F:\test.txt"); if (file.Exists) { StreamWriter sw = new StreamWriter(file.OpenWrite()); // 或者StreamWriter sw = new StreamWriter("@"F:\test.txt" ,true); sw.WriteLine("Hello Sirius!!"); sw.Close(); } else { Console.WriteLine("Not Exist"); } Console.Read(); } }}
4)StreamReader
使用StreamReader和上面的StreamWriter差不多,很方便。
using System;using System.Collections.Generic;using System.IO;using System.Linq;using System.Text;using System.Threading.Tasks;namespace FileTask{ class Program { static void Main(string[] args) { FileInfo file = new FileInfo(@"F:\test.txt"); if (file.Exists) { StreamReader sr = new StreamReader(file.OpenRead()); String line = sr.ReadLine(); Console.WriteLine(line); } else { Console.WriteLine("Not Exist"); } Console.Read(); } }}
0 0
- C#读写INI文件
- C#读写INI文件
- C#读写INI文件
- C#读写INI文件
- c#读写xml文件
- c#读写xml文件
- c#读写xml文件
- C#读写文件
- C# 文件读写
- C#读写xml文件
- c#读写ini文件
- C#读写INI文件
- c#读写xml文件
- C#读写xml文件
- c# 读写xml文件
- C#读写xml文件
- C#读写xml文件
- C#读写中文文件
- 升序定时器的时间链表的完全实现
- 【C语言天天练(二一)】内联函数
- mysql中key 、primary key 、unique key 与index区别
- 【甘道夫】Eclipse+Maven搭建HBase开发环境及HBaseDAO代码示例
- Android 命令窗口中adb常用命令
- C#文件读写
- XML中的转义字符
- Android Tween动画之RotateAnimation实现图片不停旋转
- Android学习笔记(七)之Android socket通信-解决中文乱码
- mysql中外键的作用和举例
- VISUAL STUDIO 2008 WINDOWS FORM项目发布生成安装包详解(转)
- MySQL外键的设置及作用
- Python 学习笔记 6
- 瑞柏匡丞:别样的LBS定位服务