The FileStream object represents a stream that points to a file on a disk or network path. This class provides a way to read and write bytes in a file, but often uses StreamReader or StreamWriter to perform these functions. This is because the FileStream class operates on byte and byte arrays, and the Stream class operates on character data. Character data is easy to use, but some operations, such as random file access (accessing data at a point in the middle of a file), must be performed by the FileStream object.
The simplest constructor for creating a FileStream object is as follows:
1 FileStream file = new FileStream (filename,filemode.member);
2 FileStream file = new FileStream (FileName, Filemode.member, Fileaccess.member), and FileAccess member:
Members |
Description |
Read |
Open file for read-only |
Write |
Open file for write-only |
ReadWrite |
Open file for read-write |
The action specified on the file not FileAccess enumeration member causes an exception to be thrown. The purpose of this property is to change the user's access to the file based on the user's level of authentication.
In a version of the FileStream constructor that does not use the FileAccess enumeration parameter, the default value FileAccess is used. ReadWrite.
FileMode enumeration members, what happens with each value, depends on whether the specified file name represents the existing files.
Members |
File exists |
File does not exist |
Append |
Opens the file, flows to the end of the file, and can only be used in conjunction with enumeration FileAccess.Write |
Create a new file. Can only be used in conjunction with enumeration FileAccess.Write |
Create |
Delete the file, and then create a new file |
Create a new file |
CreateNew |
Throw exception |
Create a new file |
Open |
Open an existing file, and the stream points to the beginning of the file |
Throw exception |
OpenOrCreate |
Open file, stream point to the beginning of the file |
Create a new file |
Truncate |
Open an existing file and clear its contents. Stream points to the beginning of the file, preserving the initial creation date of the file |
Throw exception |
The FileStream class operates on byte and byte arrays, while the Stream class operates on character data
StreamWriter allows characters and strings to be written to the file, which handles the underlying transformations and writes data to the FileStream object. StreamReader is similar.
Instance:
1using System;
2using System.Data;
3using System.IO;
4using System.Text;
5
6<summary>
7///Summary description for Filereadandwrite
8///</summary>
9public class Filereadandwrite
10{
Public Filereadandwrite ()
12{
13//
//Todo:add constructor logic here
15//
16}
17<summary>
18///write files with FileStream
</summary>
///<param name= "str" ></param>
///<returns></returns>
public void Filestreamwritefile (String str)
23{
Byte[] Bydata;
Char[] Chardata;
-Try
27{
FileStream nFile = new FileStream ("Love.txt", FileMode.Create);
29//Get character array
Chardata = str. ToCharArray ();
31//Initialize byte array
Bydata = new Byte[chardata.length];
33//Convert a character array to the correct byte format
Encoder enc = Encoding.UTF8.GetEncoder ();
Enc. GetBytes (chardata, 0, chardata.length,bydata,0,true);
Nfile.seek (0, Seekorigin.begin);
Panax Notoginseng nfile.write (bydata, 0, bydata.length);
38}
(Exception ex)
40{
A-throw ex;
42}
43}
44<summary>
///FileStream read file
</summary>
///<param name= "FilePath" ></param>
//<returns></returns>
-public string Filestreamreadfile (string filePath)
50{
Wuyi byte[] data = new BYTE[100];
char[] Chardata = new char[100];
Try
54{
FileStream file = new FileStream (FilePath, FileMode.Open);
56//File pointer pointing to 0 position
File. Seek (0, Seekorigin.begin);
58//Read in 200 bytes
The file. Read (data, 0, 200);
60//Extract byte array
Decoder Dec = Encoding.UTF8.GetDecoder ();
The Dec. GetChars (data, 0, data. Length, Chardata, 0);
63}
Up to catch (Exception ex)
65{
N-a throw ex;
67}
Convert.ToString return (Chardata);
69}
70<summary>
///StreamWriter Write file
</summary>
public void Streamwriterwritefile ()
74{
Try
76{
FileStream nFile = new FileStream ("Love.txt", filemode.createnew);
StreamWriter writer = new StreamWriter (nFile);
79
Writer. WriteLine ("I love you!");
Bayi writer. WriteLine ("Do Your Love me!");
The writer. Close ();
83}
Catch
85{ }
86}
87<summary>
StreamReader///Read file
</summary>
<returns></returns>
Streamreaderreadfile public string ()
92{
str= string "";
94 Try
95{
FileStream file = new FileStream ("Love.txt", FileMode.Open);
StreamReader sr = new StreamReader (file);
98 while (Sr. ReadLine ()!=null)
99{
+ + + Sr. ReadLine ();
101}
102//or str = Sr. ReadToEnd ();
103 Sr. Close ();
104}
Catch
106 {}
107 return STR;
108}
109}
the
FileStream read-write file "StreamWriter and StreamReader"