简体   繁体   中英

reading from file and writing array to file - C#

Is there any way to add a string array to file without using loop? I tried doing that with StreamWriter (writer) after spliting the string to cells in array. I seek for a way to avoid the for loop and the close() method.

Thanks!

if (data[0] != null)
{
    for (int i = 0; i < data.Length; i++)
    {
        this.writer.Write(this.data[i] + "+++", true);
    }
}
this.writer.Write("\n", true);
writer.Close();

I don't know why you want to write like this, but you can try:

var stringToSave = string.Join("+++", data);
File.WriteAllText(stringToSave);

But of course there is a loop somewhere in string.Join() here. I would also like to ask you, are you sure you don't want to use:

File.WriteAllLines(data);

? This will write every string of your collection to a seperate line...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM