上面主要讲了对路径和目录的操作,下面来看看对文件的操作。

文本文件编码,文本文件文件有不同的存储方式,将字符串以什么样的形式保存为二进制,这个就是编码,UTF-8、ASCII、Unicode等,如果出现乱码一般就是编码的问题,文本文件相关的函数一般都有一个Encoding类型的参数,取得乱码的方式:Encoding.Default、Encoding.UTF-8、Encoding.GetEncoding("GBK")

什么是文本文件。拖到记事本中还能看得懂的就是文本文件,doc不是。

File类的常用静态方法:(FileInfo)

void AppendAllText(string path,string contents),将文本contents附加到文件path中

bool Exists(string path),判断文件是否存在

string[] ReadAllLines(string path),读取文本文件到字符串数组中,默认为读取Unicode格式的

string ReadAllText(string path) 读取文本文件到字符串中

void WriteAllText(string path,string contents)  将文本contents保存到文件path中,会覆盖旧内容。

WriteAllLines(string path,string[] contents),将字符串数组逐行保存到文件path中,会覆盖旧内容。

 1  static void Main(string[] args)
 2         {
 3             //File.AppendAllText("c:\\2.txt","1231阿斯蒂芬");//参数 (文件路径 文件内容)
 4 
 5             if (!File.Exists("c:\\2.txt"))
 6             {
 7                 File.WriteAllText("c:\\2.txt", "abc刘嘉玲控件");//如果原来文件存在,会覆盖掉原来的文件
 8             }
 9            //File.WriteAllLines(
10 
11             FileInfo fi = new FileInfo("c:\\3.txt");//FileInfo是一个实例类,里面有很多的属性和方法
12           
13             Console.ReadLine();
14         }

下面来看下IO下面的另外一些类,文件流

StreamReader:

 

 1  static void Main(string[] args)
 2         {
 3             //using (FileStream fs = File.OpenRead("c:\\abc.txt"))
 4             //{
 5             //    using (StreamReader sr = new StreamReader(fs,Encoding.UTF8))
 6             //    {
 7             //        Console.WriteLine(sr.ReadToEnd());
 8             //    }
 9             //}
10 
11             using (FileStream fs = File.OpenRead("c:\\abc.txt"))
12             {
13                 using (StreamReader sr = new StreamReader(fs, Encoding.UTF8))
14                 {
15                     string line;
16                     while ((line = sr.ReadLine()) != null)
17                     {
18                         Console.WriteLine(line);
19                     }
20                 }
21             }
22 
23             Console.Read();
24         }

StreamWriter:

 static void Main(string[] args)
        {
            string str = "aa";

            //using (FileStream fs = File.OpenWrite(@"c:\abc.txt"))
            //{
            //    using (StreamWriter sw = new StreamWriter(fs))
            //    {
            //        sw.Write(str);
            //    }
            //    //让缓冲区的数据写入流
            //    //sw.Flush();
            //    //sw.Close();
            //    //sw.Dispose();
            //}

            List<string> list = new List<string>();
            list.Add("bb");
            list.Add("cc");
            list.Add("ss");

            using (FileStream fs = File.OpenWrite("names.txt"))
            {
                using (StreamWriter sw = new StreamWriter(fs, Encoding.UTF8))
                {
                    foreach (string item in list)
                    {
                        sw.WriteLine(item);
                    }
                }
            }

            Console.Read();
        }

FileSystemWatcher:文件监视

 FileSystemWatcher fsw=new FileSystemWatcher();

fsw.Path=@"C:\";  监视文件的路径

fsw.Filter="*.txt";   //监视文件的类型

fsw.IncludeSubdirectories=true;  //监视子文件夹

fsw.Deleted+=new FileSytemEventHandler(fsw_Deleted);  

fsw.EnableRaisingEvents=true;  //启用监视

 

posted on 2012-12-22 00:42  DREAM.T  阅读(198)  评论(0)    收藏  举报