文件操作的思考练习

文件操作1:

using System;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace FileReadWrite
{
    class Program
    {
        static void Main()
        {
            string path = "c:\\filerw.txt";
            try
            {
                if (File.Exists(path))
                    File.Delete(path);
                FileStream fs = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite);
                string message = "C#学习笔记,在博客园中留下学习历程!";
                byte[] byteMessage = new UTF8Encoding(true).GetBytes(message);//UTF8Encoding.GetBytes (String)  在派生类中重写时,将指定的 String 中的所有字符编码为一个字节序列。  
                //将指定的 String 中的所有字符编码为一个字节序列。 有关 Unicode 编码、字节顺序和字节顺序标记的更多信息,请参见 www.unicode.org 上的“The Unicode Standard”(Unicode 标准)部分。
                /*
                  byte 关键字代表一种整型,该类型按下表所示存储值:
                                    类型  范围       大小               .NET Framework 类型  
                                    byte  0 到 255     无符号 8 位整数      System.Byte 
                 */
                /*
                初始化 UTF8Encoding 类的新实例。参数指定是否提供一个 Unicode 字节顺序标记。 
                UTF8Encoding (Boolean) 参数如果为 true,则指定提供 Unicode 字节顺序标记;否则为 false。
                */
                fs.Write(byteMessage, 0, byteMessage.Length);//常规的数组定义方式实际上就是Array的实例,因此常规的数组定义可以用到Array类的所有方法和属性。
                fs.Flush();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.ReadLine();


        }
    }
}

 

文件操作2:

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace FileWriteRead
{
    class Program
    {
        static void Main()
        {
            string path = "c:\\filerw.txt";
            try
            {
                if (!File.Exists(path))
                    Console.WriteLine("文件不存在!");
                else
                {
                    int byteNumber;
                    FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);//也可以写成下面这两句,效果是一样的
                    //FileStream fs;
                    //fs=File.Open(path,FileMode.Open,FileAccess.Read);
                    byte[] byteBuffer = new byte[100];
                    while ((byteNumber = fs.Read(byteBuffer, 0, byteBuffer.Length)) != 0)
                    {
                        Console.WriteLine(new UTF8Encoding(true).GetString(byteBuffer));//UTF8Encoding.GetString (Byte[])  在派生类中重写时,将指定字节数组中的所有字节解码为一个字符串。  
                        //如果不用下面的语句,也能读出文件,不过由于缓冲不能被清零,当最后一段内容覆盖至缓冲时,可能不能正好完全覆盖缓冲上一次的所有内容,这样读出并在此打出的文件,可能会在文件本应结尾处多出一段上一次写入缓冲的文件内容。
                        //byteBuffer=new byte[100];
                        Array.Clear(byteBuffer, 0, byteBuffer.Length);//用此句更合适(将 Array 中的一系列元素设置为零、false 或 空引用),上一句虽然能实现,但不断创建byte数组的实例,占用资源较多,也使创建的很多实例很快就没有了用处。
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
            Console.ReadLine();
        }
    }
}
 
 
posted on 2009-10-02 14:55  友闻语上  阅读(187)  评论(0)    收藏  举报