Click here to Skip to main content
15,886,110 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to write data to a file. txt, and each section of data save in different array elements. How do I do? Thanks!
Example: I have a data.txt with contents

Segment1:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa//n
Segment2:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb//n
Segment3:cccccccccccccccccccccccccccccccccccccccccccccccccccc//n

and a array: String[] parts = new String[]{....};

When writte to file data.txt.I want save, only illustrated:
parts[0]=Segment1;
parts[1]=Segment2;
.................
Posted
Updated 22-Dec-10 16:34pm
v2
Comments
Sergey Alexandrovich Kryukov 22-Dec-10 22:11pm    
There is no question -- yet. What have you tried? What's the problem?
Frankly, it's easier to read about Stream and StreamWriter and just implement is then writing and reading all those words needed to conduct question and provide answer...

My advice: just do it, and ask another question if you have any problems.
JOAT-MON 22-Dec-10 22:15pm    
I am not clear on what you are asking. Could you try to rephrase the question?

Since you are working with a string array, you can use a StreamWriter and loop through the array using the WriteLine() for each element:
C#
using ( StreamWriter writer = new StreamWriter( new FileStream(path, FileMode.OpenOrCreate)))
{
     foreach(string s in parts)
     {
          writer.WriteLine(s);
     }
}

This will ensure that each element is written out to its own line.
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900