Asp.net c # read data files created

Source: Internet
Author: User
Tags httpcontext

Asp tutorial. net c # read data files created

 

<% @ Webhandler language = "c #" class = "averagehandler" %>

Using system;
Using system. web;

Public class averagehandler: ihttphandler
{
Public bool isreusable
{Get {return true ;}}
Public void processrequest (httpcontext ctx)
{
Ctx. response. write ("hello ");
}
}

Cs files

Using system. web
Public sealed class textbuilder: ihttphandler
{
Public void processrequest (httpcontext context)
    {
Context. response. clearcontent ();
Context. response. contenttype = "text/plain ";
Context. response. write ("hello world ");
Context. response. end ();
    }
 
Public bool isreusable
    {
Get {return true ;}
    }
}

Method 2
Binarywriter and binaryreader are used to read and write data, rather than strings. The following code example shows how to write data to and read data from a new, empty file stream (test. data. After a data file is created in the current directory, binarywriter and binaryreader are created at the same time. binarywriter is used to test. data writes integers 0 to 10, test. data leaves a file pointer at the end of the file. After setting the file pointer back to the initial position, binaryreader reads the specified content.
[C #]

Using system;
Using system. io;
Class mystream {
Private const string file_name = "test. data ";
Public static void main (string [] args ){
// Create the new, empty data file.
If (file. exists (file_name )){
Console. writeline ("{0} already exists! ", File_name );
Return;
  }
Filestream fs = new filestream (file_name, filemode. createnew );
// Create the writer for data.
Binarywriter w = new binarywriter (fs );
// Write data to test. data.
For (int I = 0; I <11; I ++ ){
W. write (int) I );
  }
W. close ();
Fs. close ();
// Create the reader for data.
Fs = new filestream (file_name, filemode. open, fileaccess. read );
Binaryreader r = new binaryreader (fs );
// Read data from test. data.
For (int I = 0; I <11; I ++ ){
Console. writeline (r. readint32 ());
W. close ();
  }
  }
}


If test. data already exists in the current directory, an ioexception is thrown. Always use filemode. create to create a new file without causing ioexception.
Method 4

Using system. web
Public sealed class textbuilder: ihttphandler
{
Public void processrequest (httpcontext context)
    {
Context. response. clearcontent ();
Context. response. contenttype = "text/plain ";
Context. response. write ("hello world ");
Context. response. end ();
    }
 
Public bool isreusable
    {
Get {return true ;}
    }
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.