Mono Class Library: System.IO NamespaceSystem.IO.FileStream Class |
See Also: FileStream Members
System.Object
System.MarshalByRefObject
System.IO.Stream
System.IO.FileStream
|
All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe.
System.IO.FileStream is used for reading and writing files on a file system, as well as other file-related operating system handles such as pipes, standard input, standard output. System.IO.FileStream buffers input and output for better performance.
The System.IO.FileStream class can open a file in one of two modes, either synchronously or asynchronously, with significant performance consequences for the synchronous methods (FileStream.Read(Byte[], int, int) and FileStream.Write(Byte[], int, int)) and the asynchronous methods (FileStream.BeginRead(Byte[], int, int, AsyncCallback, object) and FileStream.BeginWrite(Byte[], int, int, AsyncCallback, object) ). Both sets of methods will work in either mode; however, the mode will affect the performance of these methods. System.IO.FileStream defaults to opening files synchronously, but provides a constructor to open files asynchronously.
When accessing files, a security check is performed when the file is created or opened. The security check is typically not done again unless the file is closed and reopened.
Note: Checking permissions when the file is first accessed minimizes the impact of the security check on application performance (since opening a file happens once, while reading and writing can happen multiple times).Note that if an opened file is passed to an untrusted caller, the security system can, but is not required to prevent the caller from accessing the file.System.IO.FileStream objects support random access to files using the FileStream.Seek(long, SeekOrigin) method, and the Stream.CanSeek properties of System.IO.FileStream instances encapsulating files are set to true. The FileStream.Seek(long, SeekOrigin) method allows the read/write position to be moved to any position within the file. This is done with byte offset reference point parameters. The byte offset is relative to the seek reference point, which can be the beginning, the current position, or the end of the underlying file, as represented by the three values of the System.IO.SeekOrigin enumeration.
If a System.IO.FileStream encapsulates a device that does not support seeking, its FileStream.CanSeek property is false.
Note: For additional information, see Stream.CanSeek.Note: The System.IO.File class provides methods for the creation of System.IO.FileStream objects based on file paths. The System.IO.MemoryStream class creates a stream from a byte array and functions similarly to a System.IO.FileStream.[Edit]
The following example demonstrates the use of a System.IO.FileStream object.
C# Example using System; using System.IO; class Directory { public static void Main(String[] args) { FileStream fs = new FileStream("log.txt", FileMode.OpenOrCreate, FileAccess.Write); StreamWriter w = new StreamWriter(fs); w.BaseStream.Seek(0, SeekOrigin.End); // Set the file pointer to the end. Log ("Test1", w); Log ("Test2", w); w.Close(); // Close the writer and underlying file. fs = new FileStream("log.txt", FileMode.OpenOrCreate, FileAccess.Read); StreamReader r = new StreamReader(fs); r.BaseStream.Seek(0, SeekOrigin.Begin); DumpLog (r); } public static void Log (String logMessage, StreamWriter w) { w.Write("Log Entry : "); w.WriteLine("{0} {1}", DateTime.Now.ToLongTimeString(), DateTime.Now.ToLongDateString()); w.WriteLine(":"); w.WriteLine(":{0}", logMessage); w.WriteLine ("-------------------------------"); w.Flush(); } public static void DumpLog (StreamReader r) { while (r.Peek() > -1) { // While not at the end of the file, write to standard output. Console.WriteLine(r.ReadLine()); } r.Close(); } }Some example output is
Log Entry : 9:26:21 AM Friday, July 06, 2001
:
:Test1
-------------------------------
Log Entry : 9:26:21 AM Friday, July 06, 2001
:
:Test2
-------------------------------
Namespace: System.IO
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0