filestream

filestream

计算机程序
FileStream对象表示在磁盘或网络路径上指向文件的流。文件流,既支持同步读写操作,也支持异步读写操作。命名空间:System.IO;程序集:mscorlib(在mscorlib.dll中)。属性:CanRead 判断当前流是否支持读取,返回bool值,True表示可以读取[1];CanWrite 判断当前流是否支持写入,返回bool值,True表示可以写入。
  • 中文名:
  • 外文名:filestream
  • 别名:
  • 表达式:
  • 提出者:
  • 适用领域:计算机
  • 语法:Visual Basic
  • 用法:Dim instance As FileStream
  • 属性:CanRead 判断当前流是否支持读取,返回bool值,True表示可以读取;判断当前流是否支持写入,返回bool值,True表示可以写入

语法

VisualBasic

_

Public Class FileStream

Inherits Stream

Visual Basic

用法

Dim instance As FileStream

C#

[ComVisibleAttribute(true)]

public class FileStream : Stream

C++

[ComVisibleAttribute(true)]

public ref class FileStream : public Stream

J#

/** @attribute ComVisibleAttribute(true) */

public class FileStream extends Stream

JScript

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^info = (gcnew UTF8Encoding( true ))->GetBytes( value );

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

}

int main()

{

String^ path = "c:tempMyTest.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, "rnand this is on a new line" );

AddText( fs, "rnrnThe following is a subset of characters:rn" );

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, "rn" );

}

}

}

finally

{

if ( fs )

delete (IDisposable^)fs;

}

}

//Open the stream and read it back.

{

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

try

{

array^b = gcnew array(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.Text.*;

class Test

{

public static void main(String[] args)

{

String path = "c:tempMyTest.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, "rnand this is on a new line");

AddText(fs,

"rnrnThe following is a subset of characters:rn");

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, "rn");

}

}

}

finally {

fs.Dispose();

}

}

//Open the stream and read it back.

{

FileStream fs = File.OpenRead(path);

try {

ubyte b[] = new ubyte;

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

相关词条

相关搜索

其它词条