收录查询

[原创---------大话: ASP.NET技术内幕](2): 迟到的总是类

续上节

(1)记住: 迟到的总是类
   例如: 你想进行文件的操作,你需要使用System.IO.File;
   方法: --------下面示例--------
   解释: 最后的(迟到的)一个是"类名"

看下MSDN上的代码:

http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/cpref/html/frlrfsystemiofilestreamclasstopic.asp

示例
[C#, C++] 下面的示例演示一些 FileStream 构造函数的用法。

[C#]
using System;
using System.IO;
using System.Text;

class Test
{
        public static void Main()
    {
        string path = @"c:\temp\MyTest.txt";

        // Delete the file if it exists.
        if (File.Exists(path))
        {
            File.Delete(path);
        }

        //Create the file.
        using (FileStream fs = File.Create(path))
        {
            AddText(fs, "This is some text");
            AddText(fs, "This is some more text,");
            AddText(fs, "\r\nand this is on a new line");
            AddText(fs, "\r\n\r\nThe following is a subset of characters:\r\n");

            for (int i=1;i < 120;i++)
            {
                AddText(fs, Convert.ToChar(i).ToString());

                //Split the output at every 10th character.
                if (Math.IEEERemainder(Convert.ToDouble(i), 10) == 0)
                {
                    AddText(fs, "\r\n");
                }
            }
        }

        //Open the stream and read it back.
        using (FileStream fs = File.OpenRead(path))
        {
            byte[] b = new byte[1024];
            UTF8Encoding temp = new UTF8Encoding(true);
            while (fs.Read(b,0,b.Length) > 0)
            {
                Console.WriteLine(temp.GetString(b));
            }
        }
    }

    private static void AddText(FileStream fs, string value)
    {
        byte[] info = new UTF8Encoding(true).GetBytes(value);
        fs.Write(info, 0, info.Length);
    }
}

[C++]
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;
using namespace System::Text;

void AddText(FileStream* fs, String* value) {
    Byte info[] = (new UTF8Encoding(true))->GetBytes(value);
    fs->Write(info, 0, info->Length);
}

int main() {
    String* path = S"c:\\temp\\MyTest.txt";

    // Delete the file if it exists.
    if (File::Exists(path)) {
        File::Delete(path);
    }

    //Create the file.
    {
        FileStream* fs = File::Create(path);
        try {
            AddText(fs, S"This is some text");
            AddText(fs, S"This is some more text,");
            AddText(fs, S"\r\nand this is on a new line");
            AddText(fs, S"\r\n\r\nThe following is a subset of characters:\r\n");

            for (int i=1;i < 120;i++) {
                AddText(fs, __box(Convert::ToChar(i))->ToString());

                //Split the output at every 10th character.
                if (Math::IEEERemainder(Convert::ToDouble(i), 10) == 0) {
                    AddText(fs, S"\r\n");
                }
            }
        } __finally {
            if (fs) __try_cast<IDisposable*>(fs)->Dispose();
        }
    }

    //Open the stream and read it back.
    {
        FileStream* fs = File::OpenRead(path);
        try {
            Byte b[] = new Byte[1024];
            UTF8Encoding* temp = new UTF8Encoding(true);
            while (fs->Read(b,0,b->Length) > 0) {
                Console::WriteLine(temp->GetString(b));
            }
        } __finally {
            if (fs) __try_cast<IDisposable*>(fs)->Dispose();
        }
    }
}

[C#, C++] 下面的示例打开一个文件(如果文件不存在则创建该文件)并将信息追加到文件末尾。

[C#]
using System;
using System.IO;
using System.Text;

class FSOpenWrite
{
    public static void Main()
    {
        FileStream fs=new FileStream("c:\\Variables.txt", FileMode.Append, FileAccess.Write, FileShare.Write);
        fs.Close();
        StreamWriter sw=new StreamWriter("c:\\Variables.txt", true, Encoding.ASCII);
        string NextLine="This is the appended line.";
        sw.Write(NextLine);
        sw.Close();
    }
}

[C++]
#using <mscorlib.dll>

using namespace System;
using namespace System::IO;
using namespace System::Text;

int main() {
    FileStream* fs = new FileStream(S"c:\\Variables.txt", FileMode::Append, FileAccess::Write, FileShare::Write);
    fs->Close();
    StreamWriter* sw = new StreamWriter(S"c:\\Variables.txt", true, Encoding::ASCII);
    String* NextLine=S"This is the appended line.";
    sw->Write(NextLine);
    sw->Close();
}

[JScript] 没有可用于 JScript 的示例。若要查看 Visual Basic、C# 或 C++ 示例,请单击页左上角的“语言筛选器”按钮 。

要求
命名空间: System.IO

平台: Windows 98, Windows NT 4.0, Windows ME, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 系列, .NET Framework 精简版

程序集: Mscorlib (在 Mscorlib.dll 中)

 

posted @ 2005-05-13 20:31  ->  阅读(235)  评论(0)    收藏  举报