Simple C # text file reading and writing

Source: Internet
Author: User
The classes in the System.IO namespace provide file and other forms of input and output for managed applications. The basic widget for managed I/O is the stream, and the stream is the abstract representation of the byte-oriented data. The stream is represented by the System.IO.Stream class.

System.IO.FileStream allows the file to be accessed as a stream;

System.IO.MemoryStream allows the memory block to be accessed as a stream ....

The most commonly used IO form for managed and unmanaged applications is file IO. The general steps for managed applications to read and write files are as follows

1. Open file with FileStream object

2. Performs binary read and write operations, wraps BinaryReader and BinaryWriter instances around the FileStream object, and invokes the BinaryReader and BinaryWriter methods to perform input and output.

3. To read and write text, wrap a StreamReader and StreamWriter around the FileStream object, and then use the StreamReader and StreamWriter methods to complete the input and output.

4, close the FileStream object.

The following is a simple text file read operation

Using System;
Using System.IO;

Class Filetest
{
static void Main (string [] args)
{
String Filename= "testfile.txt";
Open a file and display its contents
StreamReader Reader=null;
Try
{
Reader=new StreamReader (filename);
for (String Line=reader. ReadLine (); Line!=null;line=reader. ReadLine ())
Console.WriteLine (line);
}
catch (IOException E)
{
Console.WriteLine (E.message);
}
Finally
{
if (reader!=null)
Reader. Close ();
}
}
}
/**
* FCL is a very rich class library, so there are many ways to open the file and read it, such as
* 1. Create a FileStream with File.Open and wrap a StreamReader around it
* FileStream Stream=file.open (filename,filemode.open,fileaccess.read);
* StreamReader reader=new Streamreaderaa (stream);
* 2. Use File.OpenText to create a FileStream and a StreamReader in one step
* StreamReader reader=file.opentext (filename);
* Of course, there are other ways.
* To write to text, you can use the StreamWriter
*/

The exception handling in this case is to prevent unexpected occurrences, such as the file name of the constructor passed to StreamReader illegal, or the execution of Raeder. Close (); the front of the frame throws an exception.


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.