System. Io Application

Source: Internet
Author: User

Using system;
Using system. Collections. Generic;
Using system. text;
Using system. IO;

Namespace streamreader class
{
Class Program
{
// Static void main (string [] ARGs)
//{

//}
Public static void main ()
{< br> string Path = @ "C: empmytest.txt ";
try
{< br> If (file. exists (PATH)
{< br> file. delete (PATH);
}

Using (streamwriter Sw = new streamwriter (PATH) // The path parameter can be a file name, including files shared by the Unified Naming Convention (UNC. If the file already exists, it will be overwritten; otherwise, a new file will be created.
{// The path parameter is not required to be a file stored on the disk; it can be any part of the system that supports stream access.
Sw. writeline ("this"); // The writeline method inherits
Sw. writeline ("is some text ");
Sw. writeline ("to test ");
Sw. writeline ("reading ");
}

Using (streamreader sr = new streamreader (PATH ))
{
While (Sr. Peek ()> = 0) // The peek method returns the next available character, but does not use it. Return Value of the next method: the character to be read. If there are no more available characters or the Stream does not support searching, the value is-1.
{
Console. writeline (Sr. Readline (); // The Readline method overrides the base class textread
}
}
}
Catch (exception E)
{
Console. writeline ("the process failed: {0}", E. tostring ());
}
}

}
}

/*
streamreader. Readline method: Read a line of characters from the current stream and return the data as a string. Return Value of this method: The Next row in the input stream; if it reaches the end of the input stream, it is null reference (in VB, it is nothing, and in C #, It is null ).
---- inheritance relationship:
system. object
system. export albyrefobject
system. io. textreader
system. io. streamreader
system. io. stringreader
---- inheritance:
system. object
system. export albyrefobject
system. io. textwriter
system. codedom. compiler. indentedtextwriter
system. io. streamwriter
system. io. stringwriter
system. web. httpwriter
system. web. UI. htmltextwriter

The streamreader constructor must require that the specified file already exists. If it does not exist, an exception occurs.
The streamwriter constructor does not require that the specified file already exists. If it does not exist, the file will be created. If it exists, it will be rewritten or appended. This depends on the constructor you use.
In streamreader, close, Peek, read, Readline, and readtoend are all Virtual Methods in the base class textread.
In streamwriter, close, flush, and write are used to override the Virtual Methods in the base class textwriter, while writeline is used to inherit the methods in the base class textwriter (virtual ).
Generally, close the stream after a stream is used up, such as streamreader and streamwriter.

*/

 

// Using system;
// Using system. IO;
// Using system. text;

// Class test
//{
// Public static void main ()
//{
// String Path = @ "C: empmytest.txt ";

// Try
//{

/// Delete the file if it exists.
// If (file. exists (PATH ))
//{
/// Note that no lock is put on
/// File and the possibliity exists
/// That another process cocould do
/// Something with it
/// The callto exists and delete.
// File. Delete (PATH );
//}

/// create the file.
// using (filestream FS = file. create (PATH)
// {
// byte [] info = new utf8encoding (true ). getbytes ("this is some text in the file. ");
// Add some information to the file.
// FS. write (Info, 0, info. length);
///}

/// open the stream and read it back.
// using (streamreader sr = file. opentext (PATH) // file. opentext (string path) // The return type is steamreader, open an existing UTF-8 encoded text file for reading

// {
// string S = "";
// while (S = Sr. Readline ())! = NULL)
// {
// console. writeline (s);
/}< BR >//}

// Catch (exception ex)
//{
// Console. writeline (ex. tostring ());
//}
//}
//}

 

// Using system;
// Using system. IO;

// Class test
//{
// Public static void main ()
//{
// String Path = @ "C: empmytest.txt ";
// If (! File. exists (PATH ))
//{
/// Create a file to write.
// Using (streamwriter Sw = file. createtext (PATH ))
//{
// Sw. writeline ("hello ");
// Sw. writeline ("and ");
// Sw. writeline ("welcome ");
//}
//}

/// Open the file to read from.
// Using (streamreader sr = file. opentext (PATH ))
//{
// String S = "";
// While (S = Sr. Readline ())! = NULL)
//{
// Console. writeline (s );
//}
//}
//}
//}

This article from csdn blog: http://blog.csdn.net/Terry001/archive/2007/08/28/1762486.aspx

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.