C#学习笔记:文件操作

来源:互联网 发布:javascript xml dom 编辑:程序博客网 时间:2024/05/23 19:13

这里说的文件操作指的是文件的读写操作。按照文件贮存的形式,可把文件分为文本文件和二进制文件。在C#中,不同的文件类型有不同的操作方式。下面将研究在C#中如何对文本文件和二进制文件进行操作。

文本文件操作

文本文件操作主要涉及到几个类,分别是File类、FileStream类、StreamReader/StreamWriter类。要使用这几个类,首先要引用名称空间:Systerm.IO。

File类:一个静态类,不可以用来实例化对象。可以直接用来打开、创建文件,也可以对文件进行读写操作。一般会配合FileStream类一起使用。
FileStream类:非静态类。其功能是打开文件以及对文件进行读写操作,有时会与File类、StreamReader/StreamWriter类一起使用。
StreamReader类:非静态类。其功能是对文件流进行读操作,需要FileStream配合。
StreamWriter类:费静态类。其功能是对文件流进行写操作,需要FileStream配合。
下面,通过若干例子来说明这几个类的功能。

1.一次读入整个文本文件

File类对文件的读操作比较简单,就是直接读取整个文件,如果要进行更细致的操作还需要FileSream类和StreamReader类。

/*代码一:用Filele类*/string text = File.ReadAllText("my.txt");//打开文件,并读取整个文件到字符串中,读取完关闭文件。byte[] btext = File.ReadAllBytes("my.txt");//打开文件,并读取整个文件到字节数组中,读取完关闭文件。string[] strs = File.ReadAllLines("my.txt");//打开文件,并读取文件所用行,并保存到字符串数组中,读取完关闭文件。/*代码二:用FileStream类和StreamReader类*/FileStream fs = new FileStream("my.txt", FileMode.Open, FileAccess.Read);//或者FileStream fs=File.Open("my.txt", FileMode.Open, FileAccess.Read);StreamReader sr = new StreamReader(fs);string str= sr.ReadToEnd();//从流的当前位置读取到文件末尾

2.一次从文件读取一行

如果要对文件进行行操作的话,就需要用到FileStream类和StreamReader类了。

FileStream fs = new FileStream("my.txt", FileMode.Open, FileAccess.Read);StreamReader sr = new StreamReader(fs);string str= sr.ReadLine();//从文件当前位置开始读取一行

3.一次从文件读取一块

对文件进行块操作,可以用FileStream类和StreamReader类。

/*代码一:仅用FileStream类*/FileStream fs = new FileStream("my.txt", FileMode.Open, FileAccess.Read);byte[] bytes=new byte[100];int num = fs.Read(bytes, 10, 100);//从文件的第十个字节开始读取100个字节到数组bytes中。/*代码二:FileStream类和StreamReader类*/FileStream fs = new FileStream("my.txt", FileMode.Open, FileAccess.Read);StreamReader sr = new StreamReader(fs);char[] chs = new char[100];int num = sr.ReadBlock(chs, 10, 100);//从文件的第十个字节开始读取100个字节到数组chs。

4.从文件中读取一个字符

可以用FileStream类和StreamReader类读取文件中的一个字符。

FileStream fs = new FileStream("my.txt", FileMode.Open, FileAccess.Read);byte ch;ch=fs.ReadByte();//从流的当前位置读取一个字节。//读取指定位置的一个字节fs.Seek(2, SeekOrigin.Begin);//文件指针设定在距离开头2字节处ch=fs.ReadByte();

5.设置文件指针

有时候,从文件中读取数据既不是从文件开头开始,也不是从当前位置开始,而是从特定的位置开始,这就需要设置文件指针了。FileStream类的seek方法就是用来设置文件当前指针的。seek方法的第一个参数是偏移量,第二个参数是偏移量的参照位置(SeekOrigin.Begin、SeekOrigin.Current、SeekOrigin.End),分别是文件开头、文件当前位置、文件末尾。

FileStream fs = new FileStream("my.txt", FileMode.Open, FileAccess.Read);byte ch;//文件指针移动到距离问价开头2自己的位置,然后读取一个字节。fs.Seek(2, SeekOrigin.Begin);fs.ReadByte();

6.创建一个文件

利用File类可以创建新的文件。

//创建一个文件,如果文件存在则覆盖,否则创建新的文件。File.Create("my.txt");//向新建的文件中写入数据。File.WriteAllText("my.txt", "hello world!");

7.向文本中写入数据

//写入一个字符串File.WriteAllText("my.txt", "hello world!");//按行写入字符串数组string[] strs = new string[2] { "hhhhh", "jjjjjj" };File.WriteAllLines("my.txt", strs);//写入字节数组byte[] chs = new byte[5];File.WriteAllBytes("my.text", chs);//从缓冲区中某个位置开始读取数据输入到文件中FileStream fs = new FileStream("my.txt", FileMode.Open, FileAccess.Write);byte[] array = new byte[100];fs.Write(array, 0, 100);//向文件中写入一个字节数据FileStream fs = new FileStream("my.txt", FileMode.Open, FileAccess.Write);fs.WriteByte((byte)'a');//将任意类型的数据输入到文本文件FileStream fs = new FileStream("my.txt", FileMode.Open, FileAccess.Write);StreamWriter sw = new StreamWriter(fs);//value可以是bool/byte/char/int/float/double...char[]/byte[]/string等等sw.Write(value);//向文本文件中写入一行数据FileStream fs = new FileStream("my.txt", FileMode.Open, FileAccess.Write);StreamWriter sw = new StreamWriter(fs);//value可以是bool/byte/char/int/float/double...char[]/byte[]/string等等sw.WriteLine(value);//写完之后自动换行//将数据格式化写入到文本文件FileStream fs = new FileStream("my.txt", FileMode.Open, FileAccess.Write);StreamWriter sw = new StreamWriter(fs);float x = 12.212333f;int y = 100;sw.Write("x={0},y={1}",x,y);

二进制文件操作

二进制文件操作主要用到File类、FileStream类、BinaryReader/BinaryWriter类。下面通过例子来说明。

1.读取数据

FileStream fs = new FileStream("my.data", FileMode.Open, FileAccess.Read);BinaryReader br = new BinaryReader(fs);//读取一个浮点数float x = br.ReadSingle();//读取一个整数int y = br.ReadInt32();

2.写入数据

FileStream fs = new FileStream("my.data", FileMode.Open, FileAccess.ReadWrite);float x = 12.33f;int y = 100;BinaryWriter bw = new BinaryWriter(fs);bw.Write(x);bw.Write(y);

注意事项

需要注意的是,每次打开文件,操作完毕后,需要关闭文件,否则写入到流的数据可能不会被保存到文件中。如有必要,可以使用Flush方法立即将流中的数据写入到文件中。例如

FileStream fs = new FileStream("my.txt", FileMode.Open, FileAccess.ReadWrite);StreamWriter sw = new StreamWriter(fs);sw.Write(111111);sw.Flush();//将数值111111l立即保存到文件中sw.Write("hello");sw.Flush();//将字符串立即保存到文件中。sw.Close();//关闭流写入器fs.Close();//关闭流
原创粉丝点击