filestream

FileStream 類是公開以檔案為主的 Stream,既支持同步讀寫操作,也支持異步讀寫操作。 命名空間:System.IO 程式集:mscorlib(在 mscorlib.dll 中)

基本介紹

  • 外文名:filestream
  • 語法:Visual Basic
  • 聲明:<ComVisibleAttribute(True)> _
  • 用法:Dim instance As FileStream
語法,Visual Basic,C#,C++,J#,JScript,備註,注意,示例,C++:,J#,C#,

語法

Visual Basic

1、聲明
<ComVisibleAttribute(True)> _
Public Class FileStream
Inherits Stream
Visual Basic
2、用法
Dim instance As FileStream

C#

1、聲明
[ComVisibleAttribute(true)]
public class FileStream : Stream

C++

1、聲明
[ComVisibleAttribute(true)]
public ref class FileStream : public Stream

J#

1、聲明
/** @attribute ComVisibleAttribute(true) */
public class FileStream extends Stream

JScript

1、聲明
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, "\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" );
}
}
}
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.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, "\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, System.Convert.ToString((char)i));
//Split the output at every 10th character.
if (Math.IEEEremainder(Convert.ToDouble(i), 10) == 0) {
AddText(fs, "\r\n");
}
}
}
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

C#

using System;
using System.IO;
using System.Text;
class Test {
publicstaticvoid Main()
{
string path = @"c:\temp\MyTest.txt"; .
if (File.Exists(path)) {
File.Delete(path);
}
.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());
if (Math.IEEERemainder(Convert.ToDouble(i), 10) == 0) {
AddText(fs, "\r\n");
}
}
}
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));
}
}
}
privatestaticvoid AddText(FileStream fs, string value) {
byte[] info = new UTF8Encoding(true).GetBytes(value);
fs.Write(info, 0, info.Length);
}
}

相關詞條

熱門詞條

聯絡我們