Streamwriter and streamreader-reading and writing files

Source: Internet
Author: User
You can use the streamwriter class to write text files:
<Table border = "1">
<Tr>
<TD colspan = "2" style = "color: #660066;">
Writing text files:
</TD>
</Tr>
<Tr>
<TD style = "font-size: 10pt">
Enter the file name:
</TD>
<TD style = "width: 158px">
<Asp: textbox id =" Filetextbox "Runat =" server "> </ASP: textbox>
</TD>

</Tr>
<Tr>
<TD style = "width: 158px">
Enter the file content:
</TD>

<TD>
<Asp: textbox id =" Contenttextbox "Runat =" server "textmode =" multiline "Height =" 216px "width =" 216px "> </ASP: textbox>
</TD>
</Tr>
<Tr>
<TD colspan = "2">
<Asp: Label id =" Msglabel "Runat =" server "> </ASP: Label>
</TD>
</Tr>
<Tr>
<TD align = "center" colspan = "2">
<Asp: button id = "surebutton" runat = "server" text = "Confirm write" onclick =" Surebutton_click "/>
</TD>
</Tr>
</Table>
Using system. IO;

Protected voidSurebutton_click(Object sender, eventargs E)
{
Try
{
If (file. exists (filetextbox. Text ))
{
Msglabel. Text = "this file already exists! ";

Return;
}
Else
{
Streamwriter Sw = file. createtext (filetextbox. Text );
Sw. writeline (Contenttextbox. Text );
Sw. Close ();
Msglabel. Text = "Write successful! ";
}
}
Catch (exception ee)
{
Msglabel. Text = "failed to write file! Cause of failure: "+ ee. tostring ();
}
}

You can use the streamreader class to read text files:
<Table border = "1">
<Tr>
<TD colspan = "2" style = "color: #660066;">
Read text files:
</TD>
</Tr>
<Tr>
<TD style = "font-size: 10pt">
Enter the file name:
</TD>
<TD style = "width: 158px">
<Asp: textbox id =" Filetextbox "Runat =" server "> </ASP: textbox>
</TD>

</Tr>
<Tr>
<TD align = "center" colspan = "2">
<Asp: button id = "surebutton" runat = "server" text = "Confirm write" onclick =" Surebutton_click "/>
</TD>
</Tr>
<Tr>
<TD colspan = "2">
<Asp: Label id ="Msglabel "Runat =" server "> </ASP: Label>
</TD>
</Tr>
<Tr>
<TD colspan = "2">
<Asp: textbox ID = "Contenttextbox "Runat =" server "textmode =" multiline "Height =" 216px "width =" 355px "> </ASP: textbox>
</TD>
</Tr>
</Table>

Using system. IO;

Protected void surebutton_click (Object sender, eventargs E)
{
Try
{
If (! File. exists (filetextbox. Text ))
{
Msglabel. Text = "this file does not exist! ";
Contenttextbox. Visible = false;

Return;
}
Else
{
Streamreader Sr = File. opentext (Filetextbox. Text );
Contenttextbox. Text = sr. Readtoend ();
Sr. Close ();
Contenttextbox. Visible = true;
Msglabel. Text = "The text file is successfully read! The content is as follows :";
}
}
Catch (exception ee)
{
Msglabel. Text = "failed to process the read text file! Cause of failure: "+ ee. tostring ();
}
}

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.