I. system. Io namespace Of the. NET Framework class library
The system. Io namespace contains the types that allow reading and writing files and data streams, as well as the types that support basic files and directories.
II. C # filestream for file read/write
1. (filestream fs1 = file. Open ("C: \ test.txt", filemode. Open ));
Filemode. Open open the file c: \ test.txt "directly using the filestream class ".
2. (filestream fs2 = file. Open ("C: \ test.txt", filemode. append, fileaccess. Write ));
Filemode. append: open the file "C: \ test.txt" in append mode and write some content to "C: \ test.txt.
3. (filestream fs3 = file. Open ("C: \ test.txt", filemode. truncate, fileaccess. readwrite )).
Filemode. truncate means to open the file and clear the content before operating the file.
4. filestream myfilestream1 = new filestream (@ "C: testing.txt", filemode. Create, fileshare. Read );
This method creates a file that can be read and written, and allows others to read the content of the file.
3. C # stream-based input and output
C # stream-based input and output.: Stream-connects to physical devices through the C # I/O system, that is, physical storage devices such as hard disks that are read and written at ordinary times. stream/stream methods and attributes:
method/properties description
void close () Close stream
void flush () content in the cleanup stream
int readbyte () returns an integer representing the number of input bytes. If no data is returned,-1 is returned.
int read (byte [] Buf, int offset, int numbytes) reads numbytes bytes to the offset value of byte [], starting position, and returns the number of successfully read bytes
long seek (long offset, seekorigin origin) locates the current position to the offset after the origin is the initial position.
void writebyte (byte B) writes a single byte to an output stream.
void write (byte [] Buf, int offset, int numbytes) writes the numbytes bytes starting from offset in byte [] Buf.
whether bool Canread is readable
whether bool canseek supports addressing
whether bool canwrite can write data
length of long length stream
long position current stream location.
Stream inheritance Structure
Stream is a very large category. When reading and writing files, you can perform professional Data Reading and Writing through different streams.
The filemode and fileaccess rules:
Value Meaning
Filemode. Create creates a file. files with the same name will be destroyed.
Filemode. createnew: Creates a new file, which does not exist before.
Filemode. Open open an existing file
Filemode. openorcreate
Filemode. truncate open an existing file and clear its content
Filemode. append writes data to the end of the file in the form of append
If you want to restrict file access permissions when opening a file, you can create the following constructor:
Filestream (string filename, filemode mode, fileaccess );
File Name mode operation mode
Access can be one of the following values:
Fileaccess. Read/fileaccess. Write/fileaccess. readwrite;
Filestreamfs = new filestream ("C: \ tab.txt", filemode. openorcreate, fileaccess. Read );
In C #, operations on stream and byte array sent by post appear to be rare. The following two sectionsCode.Function: Copy a.jpg under the same folder to generate B .jpg. The Code is as follows:
Bytearray. aspx. CS
Using system;
Using system. IO;
Using system. Data;
Using system. drawing;
Using system. Drawing. imaging;
Using system. net;
Namespace barray {
Public partial class imghandler: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Try
{
Stream sin = page. Request. inputstream;
System. Drawing. Image IMG = system. Drawing. bitmap. fromstream (SIN );
Bitmap BMP = new Bitmap (IMG );
Memorystream BMP stream = new memorystream ();
BMP. Save (BMP stream, system. Drawing. imaging. imageformat. JPEG );
Filestream FS = new filestream (system. Web. httpcontext. Current. server. mappath ("B .jpg"), filemode. Create );
BMP stream. writeto (FS );
BMP stream. Close ();
FS. Close ();
BMP stream. Dispose ();
FS. Dispose ();
Response. Write ("successful ");
}
Catch
{
Response. Write ("failed ");
}
}
}
}
Gopost. aspx. CS
Using system;
Using system. IO;
Using system. drawing;
Using system. Drawing. imaging;
Using system. net;
Using system. text;
Namespace gopost
{
Public partial class posthandler: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Postimage ();
}
Private void postimage ()
{
Try
{
Httpwebrequest request;
String imgurl = system. Web. httpcontext. Current. server. mappath ("a.jpg ");
Request = (httpwebrequest) httpwebrequest. Create (http: // localhost/bytearray. aspx );
Request. keepalive = true;
Request. method = "Post ";
Byte [] bytearray = cvtimgbarr (system. Drawing. Image) New Bitmap (@ imgurl), imageformat. JPEG );
Request. contenttype = "image/JPEG ";
Request. contentlength = bytearray. length;
Stream newstream = request. getrequeststream ();
Newstream. Write (bytearray, 0, bytearray. Length );
Newstream. Close ();
Response. Write ("image copied ");
}
Catch
{
Response. Write ("failed to copy the image ");
}
}
Private Static byte [] cvtimgbarr (system. Drawing. Image imagetoconvert, imageformat formatofimage)
{
Byte [] imarr;
Try
{
Using (memorystream myms = new memorystream ())
{
Imagetoconvert. Save (myms, formatofimage );
Imarr = myms. toarray ();
}
}
Catch (exception) {Throw ;}
Return imarr;
}
}
}
Relatively speaking, the application of byte array in HTML forms may be ignored all the time, but correct application of byte array can be greatly optimized.ProgramAnd make some unexpected results.
Conversion between C # stream and byte []
/// <Summary>
/// Convert stream to byte []
/// </Summary>
Public byte [] streamtobytes (Stream)
{
Byte [] bytes = new byte [stream. Length];
Stream. Read (bytes, 0, bytes. Length );
// Set the current stream position to the beginning of the stream
Stream. Seek (0, seekorigin. Begin );
Return bytes;
}
/// <Summary>
/// Convert byte [] to stream
/// </Summary>
Public stream bytestostream (byte [] bytes)
{
Stream stream = new memorystream (bytes );
Return stream;
}
/// <Summary>
/// Write stream to a file
/// </Summary>
Public void streamtofile (Stream stream, string filename)
{
// Convert stream to byte []
Byte [] bytes = new byte [stream. Length];
Stream. Read (bytes, 0, bytes. Length );
// Set the current stream position to the beginning of the stream
Stream. Seek (0, seekorigin. Begin );
// Write byte [] to a file
Filestream FS = new filestream (filename, filemode. Create );
Binarywriter BW = new binarywriter (FS );
Bw. Write (bytes );
Bw. Close ();
FS. Close ();
}
/// <Summary>
/// Read stream from a file
/// </Summary>
Public stream filetostream (string filename)
{
// Open the file
Filestream = new filestream (filename, filemode. Open, fileaccess. Read, fileshare. Read );
// Read the byte of the object []
Byte [] bytes = new byte [filestream. Length];
Filestream. Read (bytes, 0, bytes. Length );
Filestream. Close ();
// Convert byte [] to stream
Stream stream = new memorystream (bytes );
Return stream;
}
In addition, one application of XML is serialization, which requires converting strings into byte arrays. The method is as follows:
Byte [] bytes = system. Text. utf8encoding. utf8.getbytes (xmlcontent );
The method for converting byte arrays into strings is:
String xmlcontent = system. Text. utfencoding. utf8.getstring (bytes );
From string to stream
Byte [] buffer = system. Text. encoding. Unicode. getbytes ("Faint ");
Memorystream stream = new memorystream (buffer );
Memorystream MS = new memorystream (system. Text. encoding. Default. getbytes (aobjstr ));