如何将多行字符串写入文本文件 [英] How to write multiple line string to text file
本文介绍了如何将多行字符串写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,
我需要在文件中写入多行字符串.
我已经使用
从文本文件中读取了成功的行以字符串
Hello,
I need to write multiple line string to file.
I have readed succesfuly lines to string from textfile using
string text = sr.ReadToEnd();
然后,我将该字符串加载到文本框中.
我现在想要的是在文本框中修改多行文本并将其保存到文件中.
怎么做?
感谢您提供的任何帮助.
Then I''ve loaded that string to textbox.
What I want now is to modify multiple line text in textbox and save it to file.
How to do it?
Thanks for any kind of help.
推荐答案
如何:
How about:
string filepath = ...;
string text = File.ReadAllText(filepath);
...
File.WriteAllText(filepath, text);
这是您要的吗?
文本在读取和写入时已经包含新行-不需要特殊处理,对吗?
Is this what you ask for?
The text already contains the new lines, while reading and writing - no special handling needed, right?
除了提到解决方案3之外,您还可以尝试使用以下方法之一,
In addition to the Solution 3 mention, you could try using one of the following methods,
WriteAllLines(String, IEnumerable(Of String))
WriteAllLines(String, String())
WriteAllLines(String, IEnumerable(Of String), Encoding)
WriteAllLines(String, String(), Encoding)
WriteAllText(String, String)
WriteAllText(String, String, Encoding)
更多详细信息文件类 [
More details File Class[^]
Hope it helps :)
其他答案的重要补充:请勿使用显式的
您永远不要使用"\ r \ n",因为使用立即常量是不好的.这不是便携式的.正确的事情是使用静态属性System.Environment.NewLine
.
(顺便说一句,实际上您应该尽量避免使用所有这些立即常量,除了0、1,null之类的东西.即使字符串"是坏的;您也应该使用string.Empty
.)
请参阅: http://msdn.microsoft.com/en-us/library/system .environment.newline.aspx [ ^ ].
—SA
An important addition to other answers: don''t use explicit
You should never use "\r\n" — using immediate constants is bad; and this is not portable. Right thing is using static propertySystem.Environment.NewLine
.
(By the way, you really should avoid all those immediate constants by all means, except something like 0, 1, null. Even the string "" is bad; you should usestring.Empty
.)
Please see: http://msdn.microsoft.com/en-us/library/system.environment.newline.aspx[^].
—SA
这篇关于如何将多行字符串写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文