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

上一篇:亞當理論

下一篇:戴維南定理

相關詞條

相關搜索

其它詞條