filestream

王朝百科·作者佚名  2010-05-31
窄屏简体版  字體: |||超大  

简要介绍FileStream 类是公开以文件为主的 Stream,既支持同步读写操作,也支持异步读写操作。

命名空间:System.IO

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

语法Visual Basic1、声明

<ComVisibleAttribute(True)> _

Public Class FileStream

Inherits Stream

Visual Basic

2、用法

Dim instance As FileStreamC#1、声明

[ComVisibleAttribute(true)]

public class FileStream : StreamC++1、声明

[ComVisibleAttribute(true)]

public ref class FileStream : public StreamJ#1、声明

/** @attribute ComVisibleAttribute(true) */

public class FileStream extends StreamJScript1、声明

ComVisibleAttribute(true)

public class FileStream extends Stream

备注使用 FileStream 类对文件系统上的文件进行读取、写入、打开和关闭操作,并对其他与文件相关的操作系统句柄进行操作,如管道、标准输入和标准输出。读写操作可以指定为同步或异步操作。FileStream 对输入输出进行缓冲,从而提高性能。

FileStream 对象支持使用 Seek 方法对文件进行随机访问。Seek 允许将读取/写入位置移动到文件中的任意位置。这是通过字节偏移参考点参数完成的。字节偏移量是相对于查找参考点而言的,该参考点可以是基础文件的开始、当前位置或结尾,分别由 SeekOrigin 类的三个属性表示。

注意磁盘文件始终支持随机访问。在构造时,CanSeek 属性值设置为 true 或 false,具体取决于基础文件类型。具体地说,就是当基础文件类型是 FILE_TYPE_DISK(如 winbase.h 中所定义)时,CanSeek 属性值为 true。否则,CanSeek 属性值为 false。

虽然同步方法 Read 和 Write 以及异步方法 BeginRead、BeginWrite、EndRead 和 EndWrite 在同步或异步模式下都可以工作,但模式会影响这些方法的性能。FileStream 默认情况下以同步方式打开文件,但提供 FileStream(String,FileMode,FileAccess,FileShare,Int32,Boolean) 构造函数以异步方式打开文件。

如果进程因文件的一部分锁定而终止或者关闭具有未解除锁定的文件,则行为是未定义的。

请确保对所有 FileStream 对象调用 Dispose 方法,特别是在磁盘空间有限的环境中。如果没有可用的磁盘空间并且在终止 FileStream 之前没有调用 Dispose 方法,则执行 IO 操作会引发异常。

有关目录和其他文件操作的信息,请参见 File、Directory 和 Path 类。File 类是实用工具类,所带静态方法主要用于根据文件路径和标准输入、标准输出以及标准错误设备创建 FileStream 对象。MemoryStream 类通过字节数组创建流,而且功能与 FileStream 类似。

下表列出了其他典型或相关的 I/O 任务的示例。

若要执行此操作...

请参见本主题中的示例...

创建文本文件。

如何:向文件写入文本

写入文本文件。

如何:向文件写入文本

读取文本文件。

如何:从文件读取文本

向文件中追加文本。

如何:打开并追加到日志文件

File.AppendText

FileInfo.AppendText

重命名或移动文件。

File.Move

FileInfo.MoveTo

删除文件。

File.Delete

FileInfo.Delete

复制文件。

File.Copy

FileInfo.CopyTo

获取文件大小。

FileInfo.Length

获取文件属性。

File.GetAttributes

设置文件属性。

File.SetAttributes

确定文件是否存在。

File.Exists

读取二进制文件。

如何:对新建的数据文件进行读取和写入

写入二进制文件。

如何:对新建的数据文件进行读取和写入

检索文件扩展名。

Path.GetExtension

检索文件的完全限定路径。

Path.GetFullPath

检索路径中的文件名和扩展名。

Path.GetFileName

更改文件扩展名。

Path.ChangeExtension

流位置更改检测

如果 FileStream 对象没有独占持有其句柄,则另一个线程可以并发访问该文件句柄并更改与该文件句柄关联的操作系统的文件指针的位置。在这种情况下,FileStream 对象中的缓存位置和缓冲区中的缓存数据会受到危害。FileStream 对象会对访问缓存缓冲区的方法执行例行检查,以确保操作系统的句柄位置与 FileStream 对象使用的缓存位置相同。

如果在调用 Read 方法时检测到句柄位置发生意外更改,则 .NET Framework 会丢弃缓冲区的内容并从文件重新读取流。根据文件大小和任何其他可能影响文件流位置的进程,这可能会对性能产生影响。

如果在调用 Write 方法时检测到句柄位置发生意外更改,则丢弃缓冲区的内容并引发 IOException。

访问 SafeFileHandle 属性以公开句柄或为 FileStream 对象在其构造函数中提供 SafeFileHandle 属性时,FileStream 对象不会独占持有其句柄。

示例C++:using namespace System;

using namespace System::IO;

using namespace System::Text;

void AddText( FileStream^ fs, String^ value )

{

array<Byte>^info = (gcnew UTF8Encoding( true ))->GetBytes( value );

fs->Write( info, 0, info->Length );

}

int main()

{

String^ path = "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, "This is some text" );

AddText( fs, "This is some more text," );

AddText( fs, "

and this is on a new line" );

AddText( fs, "

The following is a subset of characters:

" );

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, "

" );

}

}

}

finally

{

if ( fs )

delete (IDisposable^)fs;

}

}

//Open the stream and read it back.

{

FileStream^ fs = File::OpenRead( path );

try

{

array<Byte>^b = gcnew array<Byte>(1024);

UTF8Encoding^ temp = gcnew UTF8Encoding( true );

while ( fs->Read( b, 0, b->Length ) > 0 )

{

Console::WriteLine( temp->GetString( b ) );

}

}

finally

{

if ( fs )

delete (IDisposable^)fs;

}

}

}J#import System.*;

import System.IO.*;

import System.Text.*;

class Test

{

public static void main(String[] args)

{

String path = "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, "This is some text");

AddText(fs, "This is some more text,");

AddText(fs, "

and this is on a new line");

AddText(fs,

"

The following is a subset of characters:

");

for (int i = 1; i < 120; i++) {

AddText(fs, System.Convert.ToString((char)i));

//Split the output at every 10th character.

if (Math.IEEEremainder(Convert.ToDouble(i), 10) == 0) {

AddText(fs, "

");

}

}

}

finally {

fs.Dispose();

}

}

//Open the stream and read it back.

{

FileStream fs = File.OpenRead(path);

try {

ubyte b[] = new ubyte[1024];

UTF8Encoding temp = new UTF8Encoding(true);

while (fs.Read(b, 0, b.length) > 0) {

Console.WriteLine(temp.GetString(b));

}

}

finally {

fs.Dispose();

}

}

} //main

private static void AddText(FileStream fs, String value)

{

ubyte info[] = (new UTF8Encoding(true)).GetBytes(value);

fs.Write(info, 0, info.length);

} //AddText

} //Test

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航