C # Read and Write TXT text

Source: Internet
Author: User

(1) Save the source object to TXT text in a certain format

Public bool savetotxtfile (list <pollutionsource> pslist)
{
Bool bresult = false;
System. Io. filestream FS = NULL;
System. Io. streamwriter Sw = NULL;
Try
{
// Create a file stream
FS = new system. Io. filestream (application. startuppath + "/data/info1.txt", system. Io. filemode. Create, system. Io. fileaccess. Write );
// Create the input stream corresponding to the file
Sw = new system. Io. streamwriter (FS );
// Write information to the input stream
Sw. Write (string. Format ("{0} \ t {1} \ t {2} \ t {3} \ t {4} \ r \ n ",
"Name", "type", "place", "date "));
Foreach (pollutionsource PS in pslist)
{
Sw. Write (string. Format ("{0} \ t {1} \ t {2} \ t {3} \ t {4} \ r \ n ",
PS. Name, PS. type, PS. Place, PS. Date ));
}
Bresult = true;
}
Catch
{
Bresult = false;
}
Finally
{
If (SW! = NULL)
{
Sw. Close ();
}
If (FS! = NULL)
{
FS. Close ();
}
}
Return bresult;
}

(2) read TXT text information

 

Public list <Tuli> readtxtfile (string colorfilename, string gradefilename)
{
List <Tuli> tulis = new list <Tuli> ();

System. Io. filestream FS = NULL;
System. Io. streamreader sr = NULL;
If (system. Io. file. exists (colorfilename) & system. Io. file. exists (gradefilename ))
{
Try
{
If (tulis = NULL)
Tulis = new list <Tuli> ();
// Read the color information
// Using Automatically releases resources
Using (FS = new system. Io. filestream (colorfilename, system. Io. filemode. Open ))
{
Sr = new system. Io. streamreader (FS, system. Text. encoding. Default );
String line = NULL;
System. Drawing. color = new system. Drawing. color ();
String [] RGB = new string [4];

For (line = Sr. Readline (); line! = NULL; line = Sr. Readline ())
{
If (! (Line. Trim () = ""))
{
Tuli = new Tuli ();
RGB = line. Split (New String [] {""}, stringsplitoptions. removeemptyentries );
Tuli. color = system. drawing. color. fromargb (convert. toint32 (RGB [0]), convert. toint32 (RGB [1]), convert. toint32 (RGB [2]);
// Tuli. value = RGB [3];
Tulis. Add (Tuli );
}
}
// Read the classification information
FS = new system. Io. filestream (gradefilename, system. Io. filemode. Open );
Sr = new system. Io. streamreader (FS, system. Text. encoding. Default );

Int I = 0;
String line1 = NULL;
For (line = Sr. Readline (); line! = NULL ;)
{
Line1 = Sr. Readline ();
If (line1! = NULL)
{
Tulis [I]. value = line + "-" + line1;
}
I ++;
Line = line1;
}
}
}
Catch (exception ex)
{
Throw (Ex );
}
Finally
{
If (SR! = NULL)
{
Sr. Dispose ();
Sr. Close ();
}
If (FS! = NULL)
{
FS. Dispose ();
FS. Close ();
}
}
}
Else
{
Tulis = NULL;
}
Return tulis;
}

 

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.