文件流重新整理-20151113

问题1

File和FileInfo的关系

System.IO包含另一个类File,它的功能与FileInfo一样,不过不同的是,File类成员为静态。所以,使用File代替FileInfo就不必实例化一个新FileInfo对象。
那么为什么有时还使用FileInfo呢?因为每次通过File类调用某个方法时,都要占用一定的cpu处理时间来进行安全检查,即使使用不同的File类的方法重复访问同一个文件时也是如此。而,FileInfo类只在创建FileInfo对象时执行一次安全检查。

//两行代码意义是一样的都是创建txt文件

FileStream fs = new FileStream("D:\\a.txt",FileMode.Create);
FileStream fstwo = File.Create("D:\\b.txt");

1.文件的写入----可用于汇出xml或者txt文本

  Open 是打开现有文件,如果文件不存在则发生异常
Create 是创建一个新文件,如果已经存在则发生异常
OpenOrCreate 则是说尝试打开已经存在的文件,如果还不存在就新建一个
 

以下两种方法可用此方法在固定位置汇出txt或者xml      

(1).使用FileStream类创建文件,然后将数据写入到文件里。

复制代码
        public void Write()
        {
FileStream fs = new FileStream("E:\\ak.txt", FileMode.Create); //获得字节数组 byte[] data = System.Text.Encoding.Default.GetBytes("Hello World!"); //开始写入 fs.Write(data, 0, data.Length); //清空缓冲区、关闭流 fs.Flush(); fs.Close(); }
复制代码

  (2).使用FileStream类创建文件,使用StreamWriter类,将数据写入到文件。

复制代码
#region 可用此方法在固定位置汇出txt或者xml       
public void Write(string path) { FileStream fs = new FileStream(path, FileMode.Create); StreamWriter sw = new StreamWriter(fs); //开始写入 sw.Write("Hello World!!!!"); //清空缓冲区 sw.Flush(); //关闭流 sw.Close(); fs.Close(); }
#endregion

 static void Main(string[] args)
        {
            //C#文件流写文件,默认追加FileMode.Append 
            string msg = "okffffffffffffffff";
            byte[] myByte = System.Text.Encoding.UTF8.GetBytes(msg);
            using (FileStream fsWrite = new FileStream(@"D:\1.txt", FileMode.Append))
            {
                fsWrite.Write(myByte, 0, myByte.Length);
            }; 
            //c#文件流读文件 
            using (FileStream fsRead = new FileStream(@"D:\1.txt", FileMode.Open))
            {
                int fsLen = (int)fsRead.Length;
                byte[] heByte = new byte[fsLen];
                int r = fsRead.Read(heByte, 0, heByte.Length);
                string myStr = System.Text.Encoding.UTF8.GetString(heByte);
                Console.WriteLine(myStr);
                Console.ReadKey();
            } 
        }

使用FileStream类进行文件的读取,并将它转换成char数组,然后输出。

 

 

复制代码
        byte[] byData = new byte[100];
        char[] charData = new char[1000];
        public void Read()
        {
            try
            {
                FileStream file = new FileStream("E:\\test.txt", FileMode.Open);
                file.Seek(0, SeekOrigin.Begin);
                file.Read(byData, 0, 100); //byData传进来的字节数组,用以接受FileStream对象中的数据,第2个参数是字节数组中开始写入数据的位置,它通常是0,表示从数组的开端文件中向数组写数据,最后一个参数规定从文件读多少字符.
                Decoder d = Encoding.Default.GetDecoder();
                d.GetChars(byData, 0, byData.Length, charData, 0);
                Console.WriteLine(charData);
                file.Close();
            }
            catch (IOException e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    
复制代码

使用StreamReader读取文件,然后一行一行的输出。

    public void Read(string path)
        {
            StreamReader sr = new StreamReader(path,Encoding.Default);
            String line;
            while ((line = sr.ReadLine()) != null) 
            {
                Console.WriteLine(line.ToString());
            }
        }
往myText.txt中追加文件

StreamWriter sw = File.AppendText(Server.MapPath(".")+"\\myText.txt");

sw.WriteLine("追逐理想");

sw.WriteLine("kzlll");

sw.WriteLine(".NET笔记");

sw.Flush();

sw.Close();
//递归删除文件及其文件夹
public void DeleteFolder(string dir) { if (Directory.Exists(dir)) //如果存在这个文件夹删除之 { foreach(string d in Directory.GetFileSystemEntries(dir)) { if(File.Exists(d)) File.Delete(d); //直接删除其中的文件 else DeleteFolder(d); //递归删除子文件夹 } Directory.Delete(dir); //删除已空文件夹 Response.Write(dir+" 文件夹删除成功"); } else Response.Write(dir+" 该文件夹不存在"); //如果文件夹不存在则提示 } protected void Page_Load (Object sender ,EventArgs e) { string Dir="D:\\gbook\\11"; DeleteFolder(Dir); //调用函数删除文件夹 }

 

posted @ 2015-11-13 01:22  好好學習  阅读(335)  评论(0编辑  收藏  举报